2015-05-29 39 views
0

我有一个使用Net :: Google :: Spreadsheets的应用程序。它在本周早些时候开始失败并出现身份验证错误。我的理解是Google已经不赞成使用某些身份验证方法,并且我们现在要使用OAuth2。Net :: Google :: Spreadsheets登录失败。需要使用双腿OAuth2

我的应用程序运行一个无头的服务器上,所以我不能使用Net::Google::AuthSub login failed with new Google Drive version

显示我需要用两条腿的认证与服务帐户的三条腿的OAuth2解决方案。我将代码放在一起构建JWT,并获取access_token(详见Google开发人员文档)。

我需要一些帮助是我需要使用获取Net :: Google :: Spreadsheets来使用此access_token的方法,或者使用此模块来执行双腿OAuth2的方法。

回答

2

如果你已经设法创建了access_token,那么你应该可以通过使用它来创建一个新的Net::OAuth2::AccessToken对象,然后或多或少地进行处理,使其能够以Net :: Google :: Spreadsheets的正确格式获得在example you linked to in the other thread

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,但是如果你每次都生成一个新的,这应该起作用。

+0

ELNJ,非常感谢您的回复。这几乎做了诀窍。这照顾了身份验证,我唯一需要做的其他事情就是与我生成的服务帐户电子邮件地址(而不是我的主要电子邮件地址)共享我的电子表格。 – NRH

+0

太棒了,很高兴它为你工作,@NRH。您能否将答案标记为“已接受”? – ELNJ

相关问题