7

Using Laravel Socailite to connect to the Google API and I am getting back a connection fine, however this access doesn't return a refresh token so my connection is timing out.

$scopes = [
            'https://www.googleapis.com/auth/webmasters',
            'https://www.googleapis.com/auth/webmasters.readonly',
            'https://www.googleapis.com/auth/analytics.readonly',
            'https://www.googleapis.com/auth/userinfo.profile',
            'https://www.googleapis.com/auth/userinfo.email',
          ];    
$parameters = ['access_type' => 'offline'];
return Socialite::driver('google')->scopes($scopes)->with($parameters)->redirect();

How do I get the refresh token back?

Ivan
  • 2,463
  • 1
  • 20
  • 28
Kieran Headley
  • 933
  • 1
  • 12
  • 21

1 Answers1

10

When you redirect your users to Google, set access_type to offline with the with() method when redirecting, like this:

return Socialite::driver('google')
    ->scopes() // For any extra scopes you need, see https://developers.google.com/identity/protocols/googlescopes for a full list; alternatively use constants shipped with Google's PHP Client Library
    ->with(["access_type" => "offline", "prompt" => "consent select_account"])
    ->redirect();
JjamesS
  • 101
  • 1
  • 3
  • 3
    By default refresh_token is returned only on first authorization, by adding `"prompt" => "consent select_account"` we force it to be returned every time. – Paul Oct 05 '17 at 10:08
  • The refresh token doesn't expire though so you'd only need it the first time right? – Dylan Buth Apr 06 '18 at 15:32