Net::Google::Spreadsheets 登录失败。需要使用两条腿的 OAuth2

Net::Google::Spreadsheets login failing. need to use two-legged OAuth2

我有一个使用 Net::Google::Spreadsheets 的应用程序。本周早些时候,它开始因身份验证错误而失败。我的理解是 Google 已经弃用了一些身份验证方法,我们现在要使用 OAuth2。

我的应用程序在无头服务器上运行,因此我无法使用

中所示的三足 OAuth2 解决方案

我需要通过服务帐户使用两步验证。我将代码放在一起构建 JWT,并获得 access_token(详见 Google 开发人员文档)。

我需要一些帮助的是我需要使用的方法来获取 Net::Google::Spreadsheets 以利用此 access_token,或者执行与此模块一起使用的双足 OAuth2 的方法.

如果您成功创建了 access_token,那么您应该能够通过使用它来创建新的 Net::OAuth2::AccessToken object, and then proceeding more or less as in the 来将其转换为 Net::Google::Spreadsheets 的正确格式:

use Net::Google::Spreadsheets;
use Net::Google::DataAPI::Auth::OAuth2;
use Net::OAuth2::AccessToken;

my $oauth2 = Net::Google::DataAPI::Auth::OAuth2->new(
    client_id => 'my_client_id.apps.googleusercontent.com',
    client_secret => 'my secret',
    scope => ['http://spreadsheets.google.com/feeds/'],
    );
my $access_token = 'token you generate somehow';

my $token = Net::OAuth2::AccessToken->new(access_token => $access_token,
                  profile => $oauth2->oauth2_webserver,
                  token_type => 'Bearer',
                  );
$oauth2->access_token($token);

my $service = Net::Google::Spreadsheets->new(auth => $oauth2);

然后您就可以使用那里的服务了。如果你想重复使用同一个标记,你还需要得到一个 refresh_token 并包含它,以及设置 auto_refresh,但如果你每次都生成一个新的,这应该工作。