vendredi 1 juillet 2016

Socialite & Google Analytics API

I am attempting to access Google Analytics data on behalf of an authenticated user.

So far, I am successfully using Socialite (OAuth 2.0) to log the user in, and then store their "token" in the session within the callback function.

I am then trying to use Google's PHP library to access their analytics data. However, I get the following error:

The OAuth 2.0 access token has expired, and a refresh token is not available. Refresh tokens are not returned for responses that were auto-approved.

Here is the code I am attempting to use (line breaks & comments for easy reading):

// New up google client from include PHP library
$client = new Google_Client();

// Set credentials createdin Google Developer Console
$client->setClientId(env('GOOGLE_CLIENT_ID'));
$client->setClientSecret(env('GOOGLE_SECRET_ID'));

// Add scopes for access
$client->addScope('http://ift.tt/1g8swHb');
$client->addScope('http://ift.tt/IX6Fa1');
$client->addScope('http://ift.tt/AjNHPk');

// Apply authenticated user's token that was returned by Google
$client->setAccessToken(json_encode(array('access_token' => Auth::user()->token)));

// Now new up a service and pass in the above client
$analytics = new Google_Service_Analytics($client);

// Try to get users' accounts and dump, but this causes the error
// Note: any call to the service will cause the same error
dd($analytics->management_accounts->listManagementAccounts());

I have also pulled the user's code, to try: $client->authenticate(Auth::user()->code); prior to passing into the new service.

But as expected, that code is "single-use" and has already been used.

Does anyone have a suggestion or a code sample of accessing any Google API after authenticating a user via Socialite (Laravel 5.2)?

Thanks so much for reading!!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire