I'm trying to get an OAuth access token to import some data into the fusion table. I'm trying to use the Google API PHP client. I have created a service account for that purpose, and am using the code, mostly from the serviceAccount example:
function access_token()
{
$client = new Google_Client();
$client->setAuthClass ('Google_OAuth2');
// ^ Don't know if this line is required,
// ^ but it fails just as well without it.
$client->setApplicationName ('Mysite.dom.ain');
$client->setAssertionCredentials (new Google_AssertionCredentials
( 'MANY-NUMBERS-LETTERS-DASHES@developer.gserviceaccount.com',
array ('https://www.googleapis.com/auth/fusiontables'),
file_get_contents ('path/to/my/privatekey.p12') ));
$client->setClientId ('NUMBERS-LETTERS-DASHES.apps.googleusercontent.com');
$client->authenticate();
// ^ Also fails equally good with and without this line.
return $client->getAccessToken();
}
A little debug output shows that $client->authenticate()
returns true
, but $client->getAcessToken()
returns null
. No exceptions are thrown. I have the feeling I'm doing something fundamentally wrong. If so, please forgive my stupidity and point me in the right direction.
I think all you did was correct, now you have two options left:
$client
to make a service call with something like thatrefreshTokenWithAssertion()
in order to get your token:For both cases I have examples in my GitHub Repo.
You don't need the authenticate() call, but you'll need to call
refreshTokenWithAssertion()
to refresh the underlying access token. If you are using the client library to make signed requests, it will lazily make this call for you if underlying access token has expired.The API requests to refresh the access_token are expensive, and have a low quota, so you'll want to cache the access_token.