[英]Google oAuth with service account for Google Coordinate
我使用以下代碼獲取Google oAuth2的訪問令牌。
有一個問題是,如果我不添加行以使用斷言刷新令牌,它將什么都不返回。
此外,此刷新令牌將不起作用,因為當我使用此令牌訪問Google協調中心時,它返回了500后端錯誤。
請糾正我。
<?
// codes for getting access token
require_once ('/libs/google-api/src/Google_Client.php');
define('OAUTH2_CLIENT_ID', "MyClientID.apps.googleusercontent.com");
define('SERVICE_ACCOUNT_NAME', "MyEmail@developer.gserviceaccount.com");
define('KEY_FILE', 'f3dc516e866bb9fd42f58f9675d2ed57502d4093-privatekey.p12');
define('EMAIL', 'myEmail');
$key = file_get_contents(KEY_FILE);
$client = new Google_Client();
$client->setApplicationName("test");
$auth=new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/coordinate'),
$key
);
$client->setClientId(OAUTH2_CLIENT_ID);
$client->setAssertionCredentials($auth);
if($client->getAuth()->isAccessTokenExpired()){
$client->getAuth()->refreshTokenWithAssertion(); // if I didn't add this line it would return nothing
}
$accessToken=$client->getAccessToken();
?>
<?
// Errors of access to Google Coordinate using refresh token
stdClass Object
(
[error] => stdClass Object
(
[errors] => Array
(
[0] => stdClass Object
(
[domain] => global
[reason] => backendError
[message] => Backend Error
)
)
[code] => 500
[message] => Backend Error
)
)
?>
<?
// The refresh token received
{
"access_token":"ya29.1.AADtN_U0EDt_5lGMWWbBSOJActF4qEmVwiQ6EyPpEzrHy_ZUoyw7HxYmsgo-sXt1qPzcce9r8hmbviCKB-zQZMAqiO9-4gWxepbsc5jR7ceSgRRR07QvIIYgFZFoFjYCdg",
"expires_in":3600,
"created":1394837729
}
?>
由於您收到acfess_token,因此錯誤不太可能出在授權步驟中。 此外,對於任何Google API無效令牌的正確答案是提供HTTP錯誤代碼401。因此在我看來,該錯誤似乎是您使用的Coordinate API所致。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.