微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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找到的令牌会话示例

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐