perl – Net :: Google :: AuthSub登录失败,新的Google云端硬盘版本

前端之家收集整理的这篇文章主要介绍了perl – Net :: Google :: AuthSub登录失败,新的Google云端硬盘版本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Perl中的这段代码工作多年,现在我的Spreadsheets登录失败,当我登录我的帐户时,我注意到切换到新的Drive版本.可能有些认证方法已被弃用?
my $auth = Net::Google::AuthSub->new;
my $response = $auth->login('LOGIN@gmail.com','PASS');
if ($response->is_success) {
     print "Hurrah! Logged in\n";
} else {
     die "Login Failed: ".$response->error."\n";
}

结果是:

Login Failed:

代码

use Net::Google::Spreadsheets;
my $service = Net::Google::Spreadsheets->new(
 username => 'LOGIN@gmail.com',password => 'PASS'
);

结果是:

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

正如我所建议的那样,我试图跳过SSL证书检查:

$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;

但这也没有用.
我能做些什么才能让它发挥作用?谢谢.

解决方法

我必须回答我的问题,因为我很乐意找到解决方案. Google更改了身份验证算法,因此我们必须使用OAuth 2.0.
您需要在以下位置创建凭据: https://console.developers.google.com/

API& auth – >凭证 – > OAuth – >客户ID – >已安装的应用程序 – >其他

并启用您的API,即:API& auth – > API – > Google Apps API>驱动API

以下代码工作正常:

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..

保存并恢复在https://gist.github.com/hexaddikt找到的令牌会话示例

原文链接:https://www.f2er.com/Perl/171681.html

猜你在找的Perl相关文章