$clientId, 'clientSecret' => $clientSecret, 'authorizationUri' => 'https://accounts.google.com/o/oauth2/v2/auth', // Where to return the user to if they accept your request to access their account. // You must authorize this URI in the Google API Console. 'redirectUri' => $redirectURI, 'tokenCredentialUri' => 'https://www.googleapis.com/oauth2/v4/token', 'scope' => $scopes, 'expiry' => \Google\Auth\OAuth2::DEFAULT_EXPIRY_SECONDS *24 ]); // The authorization URI will, upon redirecting, return a parameter called code. if (!isset($_GET['code'])) { $authenticationUrl = $oauth2->buildFullAuthorizationUri(['access_type' => 'offline']); header("Location: " . $authenticationUrl); } else { // With the code returned by the OAuth flow, we can retrieve the refresh token. $oauth2->setCode($_GET['code']); $authToken = $oauth2->fetchAuthToken(); $refreshToken = $authToken['access_token']; // The UserRefreshCredentials will use the refresh token to 'refresh' the credentials when // they expire. $_SESSION['credentials'] = new UserRefreshCredentials( $scopes, [ 'client_id' => $clientId, 'client_secret' => $clientSecret, 'refresh_token' => $refreshToken ] ); // Return the user to the home page. redirect('/google/home/index'); // header("Location: index.php"); } } }