J.T*_*J.T 6 perl google-authentication
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";
}
Run Code Online (Sandbox Code Playgroud)
结果是:
Login failed:
Run Code Online (Sandbox Code Playgroud)
和代码:
use Net::Google::Spreadsheets;
my $service = Net::Google::Spreadsheets->new(
username => 'LOGIN@gmail.com',
password => 'PASS'
);
Run Code Online (Sandbox Code Playgroud)
结果是:
Net::Google::AuthSub login failed at /usr/local/share/perl/5.18.2/Net/Google/Spreadsheets.pm line 42.
Run Code Online (Sandbox Code Playgroud)
正如我所建议的那样,我试图跳过SSL证书检查:
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;
Run Code Online (Sandbox Code Playgroud)
但这也没有用.我能做些什么才能让它发挥作用?谢谢.
J.T*_*J.T 12
我必须回答我的问题,因为我很乐意找到解决方案.Google更改了身份验证算法,因此我们必须使用OAuth 2.0.您需要在以下网址创建凭据:https://console.developers.google.com/
API和身份验证 - >凭据 - > OAuth - >客户端ID - >已安装的应用程序 - >其他
并启用您的API,即:API&auth - > API - > Google Apps API> Drive 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..
Run Code Online (Sandbox Code Playgroud)
保存并恢复在https://gist.github.com/hexaddikt上找到的令牌会话示例