Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Google Refresh Token with Laravel Socialite

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?

like image 501
Kieran Headley Avatar asked May 04 '16 14:05

Kieran Headley


People also ask

How does laravel Socialite work?

Laravel Socialite is a package developed to abstract away any social authentication complexities and boilerplate code into a fluent and expressive interface. Socialite only supports Google, Facebook, Twitter, Linked In, Github, and Bitbucket as OAuth providers.

How do I get a new refresh token OAuth2?

Because OAuth2 access expires after a limited time, an OAuth2 refresh token is used to automatically renew OAuth2 access. Click the tab for the programming language you're using, and follow the instructions to generate an OAuth2 refresh token and set up the configuration file for your client.


1 Answers

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();
like image 200
JjamesS Avatar answered Sep 21 '22 08:09

JjamesS