我一直在嘗試使用服務帳戶進行谷歌日曆認證,但沒有多少運氣。以下程序在JSON :: WebToken編碼調用期間死亡通過Perl使用OAUTH2訪問Google Calendar API
RSA.xs:178:OpenSSL錯誤:在/usr/local/share/perl/5.14.2/JSON/WebToken/Crypt/RSA上發生了錯誤的base64解碼.pm line 19.
當我拔出私鑰並嘗試使用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 => '[email protected]',
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 => '[email protected]',
},
$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");
}
我使用相同的方法訪問Google日曆,但使用完全不同的範圍:「https:// www.googleapis.com/auth/calendar」。我懷疑這會導致錯誤的base64解碼錯誤,但是一旦你認證工作,可能會導致問題。在嘗試使用腳本進行身份驗證之前,您應該在https://developers.google.com/oauthplayground/上測試所有參數。我懷疑真正的問題在於你的私鑰。您從您從開發者控制檯下載的PKCS 12存檔文件中究竟提取了它的內容? IIRC,我首先使用了錯誤的密鑰... – ThisSuitIsBlackNot 2014-09-22 14:54:12
我使用了json密鑰,而不是PKCS 12存檔。感謝操場上的小費。我看了一下,但看起來並不適合服務帳戶。仍然有用瞭解。 – 2014-09-23 10:23:37
自從我開始工作之後已經有一段時間了......我的意思是指向[日曆API參考](https://developers.google.com/google-apps/calendar/v3/reference/),它可讓您你測試了不同的請求方法(儘管它看起來對授權問題沒有幫助)。當我設置我的應用程序(這只是幾個月前)時,我不認爲JSON密鑰可用。也許嘗試PKCS 12路線?我用這個命令提取了密鑰:'openssl pkcs12 -in foo-privatekey.p12 -nocerts -nodes | openssl rsa> id_rsa',其中'foo-privatekey.p12'是下載的檔案的名稱。 – ThisSuitIsBlackNot 2014-09-23 16:49:48