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

后端 未结 3 2308
说谎
说谎 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:00

    I have to answer my question as I was happy to find a solution. Google changed their authentication algorithm, so we have to use OAuth 2.0. You will need to create Credentials at: https://console.developers.google.com/

    APIs & auth -> Credentials -> OAuth -> Client ID -> Installed application -> Other

    and enable your API i.e.: APIs & auth -> APIs -> Google Apps APIs > Drive API

    The following code works fine:

    use Net::Google::DataAPI::Auth::OAuth2;
    use Net::Google::Spreadsheets;
    use Storable; #to save and restore token for future use
    
    my $oauth2 = Net::Google::DataAPI::Auth::OAuth2->new(
        client_id => 'ID.apps.googleusercontent.com',
        client_secret => 'SECRET',
        scope => ['http://spreadsheets.google.com/feeds/'],
      );
    #you can skip URL if you have your token saved and continue from RESTORE label
    
    my $url = $oauth2->authorize_url();
    #you will need to put code here and receive token
    print "OAuth URL, get code: $url\n";
    use Term::Prompt;
    my $code = prompt('x', 'paste the code: ', '', ''); 
    my $token = $oauth2->get_access_token($code) or die;
    
    #save token for future use
    my $session = $token->session_freeze;
    store($session, 'google_spreadsheet.session');
    
    RESTORE:
    my $session = retrieve('google_spreadsheet.session');
    my $restored_token = Net::OAuth2::AccessToken->session_thaw($session,
        auto_refresh => 1,
        profile => $oauth2->oauth2_webserver,
    );
    $oauth2->access_token($restored_token);
    
    my $service = Net::Google::Spreadsheets->new(auth => $oauth2);
    # and then as before..
    

    Save and restore token session example found at https://gist.github.com/hexaddikt

提交回复
热议问题