通过Perl访问带有OAUTH2的Google Calendar API

时间:2014-09-21 15:53:06

标签: json perl google-api

我一直在尝试使用带有服务帐户的Google日历进行身份验证,但没有太多运气。以下程序在使用

进行JSON :: WebToken编码调用期间终止

RSA.xs:178:OpenSSL错误:/usr/local/share/perl/5.14.2/JSON/WebToken/Crypt/RSA.pm第19行中的错误base64解码。

当我拔出私钥并尝试使用openssl验证它时,我收到类似的错误。那么谷歌给我一个密钥(我已经仔细检查过它)或者我做错了什么?

#!/usr/bin/perl
use warnings;
use strict;
use JSON;
use JSON::WebToken;
use LWP::UserAgent;

my $private_key_string = "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n";

my $time = time;

my $jwt = JSON::WebToken->encode(
    {
        # your service account id here
        iss   => '...9ve@developer.gserviceaccount.com',
        scope => "https://www.google.com/calendar/feeds/",
        aud   => 'https://accounts.google.com/o/oauth2/token',
        exp   => $time + 3600,
        iat   => $time,
        # To access the google admin sdk with a service account
        # the service account must act on behalf of an account
        # that has admin privileges on the domain
        # Otherwise the token will be returned but API calls
        # will generate a 403
        prn => 'me@gmail.com',
    },
    $private_key_string,
    'RS256',
    { typ => 'JWT' }
);

# Now post it to google
my $ua       = LWP::UserAgent->new();
my $response = $ua->post(
    'https://accounts.google.com/o/oauth2/token',
    {   grant_type => encode_entities('urn:ietf:params:oauth:grant-type:jwt-bearer'),
        assertion  => $jwt
    }
);

unless ( $response->is_success() ) {
    die( $response->code, "\n", $response->content, "\n" );
}

2 个答案:

答案 0 :(得分:3)

我遇到了同样的问题。当我从Google Developer Console(\ u003d)下载时,我的RSA密钥末尾的base64终端已经过JSON编码。只需将其更改为简单的' ='。

答案 1 :(得分:1)

FWIW服务密钥不再受此问题的影响。