我有一个使用Perl创建的Web应用程序,并且在电子表格文档上有一个表,我想将应用程序连接到电子表格,我尝试了文档,但无法获得令牌,我所做的是:
sub authenticate {
my $oauth2 = Net::Google::DataAPI::Auth::OAuth2->new(
client_id => 'my client id',
client_secret => 'my secret code',
scope => ['http://spreadsheets.google.com/feeds/'],
);
my $url = $oauth2->authorize_url(access_type => 'offline', approval_prompt => 'force');
use Data::Dumper;
Core::Global::Logger::debug(Dumper($url));
#you will need to put code here and receive token
print "OAuth URL, get code: \n$url\n";
use Term::Prompt;
my $code = prompt('x', 'my 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' );
}
1条答案
按热度按时间a64a0gku1#
我无法让Net::Google::Spreadsheets::V4使用服务帐户并使用Net::Google::DataAPI::Auth::OAuth2来获取访问令牌,但您应该能够从WWW::Google::Cloud::Auth::ServiceAccount获取访问令牌,然后使用该令牌来授权对google drive rest api的请求。以下代码对我有效:
编辑:
要获取具有给定ID的给定工作表的单元格的值,可以使用以下url:
https://sheets.googleapis.com/v4/spreadsheets/%s/values/%s
,其中第一个%s
由工作表的ID替换,第二个%s
表示要提取的单元格区域。下面是一个示例: