简体   繁体   中英

Google oAuth with service account for Google Coordinate

I use the following code to obtain the access token of Google oAuth2.

There is an issue that if I don't add a line to refresh the token with assertion it would return nothing.

Moreover, this refresh token won't work, because when I use this token to access Google Coordinate it returned the 500 Backend Error.

Please correct me.

<?
// 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
}
?>

Since you're receiving an acfess_token it is very unlikely that the error is in the authorization steps. Moreover, the correct answer for any Google API to an invalid token is to supply HTTP error code 401. So it appears to me as if the error is in your usage of the Coordinate API.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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