Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I manage OAuth refresh tokens with Laravel?

The Socialiate plugin provides an implementation for OAuth in Laravel, but it seems to be designed for mostly for the purpose of allowing them to not have to make a user account on your own site.

I am making an application that helps manage their Youtube account, meaning the scope of the auth request is broader (which was easy to change) but I also need a refresh token (versus just an access token) for long-term access to their account.

Is there a package out there for Laravel that already handles this? I haven't been able to find one, but maybe I'm searching for the wrong thing.

If not, how should I approach this? When I write my code that interacts with Youtube's API, do I simply need to check whether the access token is expired, and if so, write a function that does an HTTP request to get a new one with the refresh token I have stored in the database? And I guess also extend Socialite to retrieve a refresh token?

I feel like there's got to be a better way that doesn't involve me re-inventing the wheel.

like image 558
user5431713 Avatar asked Nov 29 '15 20:11

user5431713


1 Answers

It's been a while since this question was last visited, and seeing that it is the first Google result, I'd like to say: This is now possible with Socialite.

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();

This will make Google return a refresh token.

like image 140
tyteen4a03 Avatar answered Oct 13 '22 14:10

tyteen4a03