加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 22:01:10 所属栏目:大数据 来源:网络整理
导读:Perl中的这段代码工作多年,现在我的Spreadsheets登录失败,当我登录我的帐户时,我注意到切换到新的Drive版本.可能有些认证方法已被弃用? my $auth = Net::Google::AuthSub-new;my $response = $auth-login('LOGIN@gmail.com','PASS');if ($response-is_succe
Perl中的这段代码工作多年,现在我的Spreadsheets登录失败,当我登录我的帐户时,我注意到切换到新的Drive版本.可能有些认证方法已被弃用?
my $auth = Net::Google::AuthSub->new;
my $response = $auth->login('LOGIN@gmail.com','PASS');
if ($response->is_success) {
     print "Hurrah! Logged inn";
} 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: $urln";
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找到的令牌会话示例

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读