Net::Google::AuthSub login failed with new Google Drive version

后端 未结 3 2309
说谎
说谎 2020-12-09 06:45

This code in Perl was working for years and now my Spreadsheets logins failed, when I logged into my account I noticed switch to a new Drive version. Probably some authentic

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-09 07:08

    That code above absolutely would not work for me. After looking around for several hours, reading the documentation and trying different things I finally ran across some code that worked. 90% of it is the same but it was the missing 10% and the accompanying explanations that made all the difference.

    http://pastebin.com/8LeMyLW4

    (Originally from this Stack Overflow post: Authenticating in a Google sheets application)

    #!/usr/bin/perl
    
    # Code to get a web-based token that can be stored 
    # and used later to authorize our spreadsheet access. 
    
    # Based on code from https://gist.github.com/hexaddikt/6738162
    
    #-------------------------------------------------------------------
    
    # To use this code:
    
    # 1. Edit the lines below to put in your own
    #    client_id and client_secret from Google. 
    # 2. Run this script and follow the directions on 
    #    the screen, which will give step you 
    #    through the following steps:
    # 3. Copy the URL printed out, and paste 
    #    the URL in a browser to load the page. 
    # 4. On the resulting page, click OK (possibly
    #    after being asked to log in to your Google 
    #    account). 
    # 5. You will be redirected to a page that provides 
    #    a code that you should copy and paste back into the 
    #    terminal window, so this script can exchange it for
    #    an access token from Google, and store the token.  
    #    That will be the the token the other spreadsheet access
    #    code can use. 
    
    
    use Net::Google::DataAPI::Auth::OAuth2;
    use Net::Google::Spreadsheets;
    use Storable; #to save and restore token for future use
    use Term::Prompt;
    
    # Provide the filename in which we will store the access 
    # token.  This file will also need to be readable by the 
    # other script that accesses the spreadsheet and parses
    # the contents. 
    
    my $session_filename = "stored_google_access.session";
    
    
    # Code for accessing your Google account.  The required client_id
    # and client_secret can be found in your Google Developer's console 
    # page, as described in the detailed instruction document.  This 
    # block of code will also need to appear in the other script that
    # accesses the spreadsheet. 
    
    # Be sure to edit the lines below to fill in your correct client 
    # id and client secret!
    my $oauth2 = Net::Google::DataAPI::Auth::OAuth2->new(
        client_id => 'your_client_id.apps.googleusercontent.com',
        client_secret => 'your_client_secret',
        scope => ['http://spreadsheets.google.com/feeds/'],
        redirect_uri => 'https://developers.google.com/oauthplayground',
                                 );
    # We need to set these parameters this way in order to ensure 
    # that we get not only an access token, but also a refresh token
    # that can be used to update it as needed. 
    my $url = $oauth2->authorize_url(access_type => 'offline',
                     approval_prompt => 'force');
    
    # Give the user instructions on what to do:
    print <get_access_token($code) or die;
    
    # If we get to here, it worked!  Report success: 
    print "nToken obtained successfully!n";
    print "Here are the token contents (just FYI):nn";
    print $token->to_string, "n";
    
    # Save the token for future use:
    my $session = $token->session_freeze;
    store($session, $session_filename);
    
    print <new(
        client_id => 'your_client_id.apps.googleusercontent.com',
        client_secret => 'your_client_secret',
        scope => ['http://spreadsheets.google.com/feeds/'],
        redirect_uri => 'https://developers.google.com/oauthplayground',
                                 );
    
    # Deserialize the file so we can thaw the session and reuse the refresh token
    my $session = retrieve($sessionfile);
    
    my $restored_token = Net::OAuth2::AccessToken->session_thaw($session,
                                    auto_refresh => 1,
                                    profile => $oauth2->oauth2_webserver,
                                    );
    
    $oauth2->access_token($restored_token);
    # Now we can use this token to access the spreadsheets 
    # in our account:
    my $service = Net::Google::Spreadsheets->new(
                             auth => $oauth2);
    

提交回复
热议问题