我使用以下代码获取Google oAuth2的访问令牌。
如果我没有添加一行来使用断言刷新令牌,则会出现一个问题,即它不会返回任何内容。
此外,此刷新令牌无效,因为当我使用此令牌访问Google Coordinate时,它返回了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
}
?>
答案 0 :(得分:0)
由于您收到acfess_token,因此错误不太可能在授权步骤中。此外,任何Google API对无效令牌的正确答案是提供HTTP错误代码401.因此,我觉得错误就在于您使用Coordinate API。