Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

thomasjohnkane's avatar

Socialite & Google Analytics API??

I am trying 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:

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

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

// Add scopes for access
$client->addScope('https://www.googleapis.com/auth/plus.profile.emails.read');
$client->addScope('https://www.googleapis.com/auth/analytics.readonly');
$client->addScope('https://www.googleapis.com/auth/userinfo.email');

// 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 users code, to try: $client->authenticate(Auth::user()->code);

But as expected, that code is already used.

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

0 likes
3 replies
NewBee's avatar

hi, I'm also in the same ship as you! Did you find any solution? Also, where are you writing this logic? In controller or somewhere else?

thomasjohnkane's avatar

Hey NewBee - I did not find a solution yet. Unfortunately, my attention has been pulled away from this item. I think it has to do with applying a refresh token correctly. Keep in mind that you only get the refresh token the first time you authenticate, so testing can be a little tricky.

I was handling the OAuth authentication within the Auth/AuthController.

Then once authenticated, I was redirecting to the dashboard and trying to pull the Google Analytics data within the index method of that controller.

Hope that helps. Let me know if you work out a solution.

Please or to participate in this conversation.