In the https://dev.to/dnsinyukov/how-to-integrate-google-calendar-api-and-friendship-with-laravel-4cok we created a project in the Google Cloud Console, as well as configured the access keys through the API. In this article we'll create a project in Laravel that will authorize through Google and save access token to database.

Let's skip creating the basic skeleton of a Laravel framework and start immediately with creating code to work with the Google API.

*/ public function __construct(Request $request, string $clientId, string $clientSecret, string $redirectUrl, array $scopes = []) { $this->request = $request; $this->clientId = $clientId; $this->redirectUrl = $redirectUrl; $this->clientSecret = $clientSecret; $this->scopes = $scopes; } /** * @return RedirectResponse * @throws \Exception */ public function redirect(): RedirectResponse { $this->request->query->add(['state' => $this->getState()]); if ($user = $this->request->user()) { $this->request->query->add(['user_id' => $user->getKey()]); } return new RedirectResponse($this->createAuthUrl()); } /** * @return User */ public function getUser(): User { if (isset($this->user)) { return $this->user; } try { $credentials = $this->fetchAccessTokenWithAuthCode( $this->request->get('code', '')); $this->user = $this->toUser($this->getBasicProfile($credentials)); } catch (\Exception $exception) { report($exception); throw new \InvalidArgumentException($exception->getMessage()); } $state = $this->request->get('state', ''); if (isset($state)) { $state = Crypt::decrypt($state); } return $this->user ->setRedirectCallback($state['redirect_callback']) ->setToken($credentials['access_token']) ->setRefreshToken($credentials['refresh_token']) ->setExpiresAt( Carbon::now()->addSeconds($credentials['expires_in'])) ->setScopes( explode($this->getScopeSeparator(), $credentials['scope'])); } abstract protected function createAuthUrl(); abstract protected function fetchAccessTokenWithAuthCode(string $code); abstract protected function getBasicProfile($credentials); abstract protected function toUser($userProfile); }

class GoogleProvider extends AbstractProvider { protected $providerName = 'google'; public function createAuthUrl(): string { return $this->getHttpClient()->createAuthUrl(); } public function redirect(): RedirectResponse { if ($redirectCallback = config('services.google.redirect_callback')) { $this->request->query->add(['redirect_callback' => $redirectCallback]); } return parent::redirect(); } protected function fetchAccessTokenWithAuthCode(string $code): array { return $this->getHttpClient()->fetchAccessTokenWithAuthCode($code); } /** * @return array */ protected function getBasicProfile($credentials) { $jwt = explode('.', $credentials['id_token']); // Extract the middle part, base64 decode it, then json_decode it return json_decode(base64_decode($jwt), true); } /** * @param Userinfo $userProfile * @return void */ protected function toUser($userProfile) { return tap(new User(), function ($user) use ($userProfile) { $user->setId($userProfile['sub']); $user->setName($userProfile['name']); $user->setEmail($userProfile['email']); $user->setPicture($userProfile['picture']); }); } /** * @return Client */ protected function getHttpClient(): Client { if (is_null($this->httpClient)) { $this->httpClient = new \Google\Client(); $this->httpClient->setApplicationName(config('app.name')); $this->httpClient->setClientId($this->clientId); $this->httpClient->setClientSecret($this->clientSecret); $this->httpClient->setRedirectUri($this->redirectUrl); $this->httpClient->setScopes($this->scopes); $this->httpClient->setApprovalPrompt(config('services.google.approval_prompt')); $this->httpClient->setAccessType(config('services.google.access_type')); $this->httpClient->setIncludeGrantedScopes(config('services.google.include_granted_scopes')); // Add request query to the state $this->httpClient->setState( Crypt::encrypt($this->request->all())); } return $this->httpClient; }}
Newsletter

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

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners