繁体   English   中英

带有Google协调中心服务帐户的Google oAuth

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM