Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 authentication methods deprecated?

my $auth = Net::Google::AuthSub->new;
my $response = $auth->login('[email protected]', 'PASS');
if ($response->is_success) {
     print "Hurrah! Logged in\n";
} else {
     die "Login failed: ".$response->error."\n";
}

The result is:

Login failed: 

And the code:

use Net::Google::Spreadsheets;
my $service = Net::Google::Spreadsheets->new(
 username => '[email protected]',
 password => 'PASS'
);

The result is:

Net::Google::AuthSub login failed at /usr/local/share/perl/5.18.2/Net/Google/Spreadsheets.pm line 42.

As suggested somewhere I tried to skip SSL certificate checking with:

$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;

but this doesn't help too. What can I do to make it work? Thanks.

like image 315
J.T Avatar asked May 27 '15 07:05

J.T


1 Answers

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

like image 54
J.T Avatar answered Sep 22 '22 16:09

J.T