logging in using google via oauth is nothing new. in this walkthrough, we'll be modifying our existing laravel 8 restful(ish) api to accept logins via google and to read data about the user's google drive via the https://github.com/googleapis/google-api-php-client.

#the-flyover the flyover in this walkthrough, we are going to: install and configure passport to protect our api endpoints install the google api php sdk build the endpoints necessary to log in with google and issue a passport bearer token call the google api to get information on the logged-in user call the google api to get data on the user's google drive supplementally, go through setting up a google project with ouath access in case we don't already have one when we're done, we should be able to let people login with google, issue bearer tokens to let them access our api, and access users' read-only google drive data from our api.

Exit fullscreen mode the high-level view of what this method does is: acquire the google auth code from the query string create a google api client call the google api to exchange the auth code for an access code by using fetchAccessTokenWithAuthCode() call setAccessToken() with the returned token so the google client knows what user it's acting on behalf of use the google clients Oauth2 service to call google and get the user's data.

/** * Returns a google client that is logged into the current user * * @return \Google_Client */ private function getUserClient():\Google_Client { /** * Get Logged in user */ $user = User::where('id', '=', auth()->guard('api')->user()->id)->first(); /** * Strip slashes from the access token json * if you don't strip mysql's escaping, everything will seem to work * but you will not get a new access token from your refresh token */ $accessTokenJson = stripslashes($user->google_access_token_json); /** * Get client and set access token */ $client = $this->getClient(); $client->setAccessToken($accessTokenJson); /** * Handle refresh */ if ($client->isAccessTokenExpired()) { // fetch new access token $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); $client->setAccessToken($client->getAccessToken()); // save new access token $user->google_access_token_json = json_encode($client->getAccessToken()); $user->save(); } return $client; } // getUserClient }
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners