/** * Returns an authorized API client * * @return Google_Client the authorized client object */ function getClient() { global $ini, $verbose; static $client = NULL; if ($client) { return $client; } if (DEBUG) { print "Google APIs application name: {$ini['gmail_application_name']}\n" . "Google Gmail client secret file: {$ini['gmail_client_secret_path']}\n" . "Google Gmail credentials path: {$ini['gmail_credentials_path']}\n"; } try { $client = new Google_Client(); $client->setApplicationName($ini['gmail_application_name']); $client->setScopes(GMAIL_SCOPES); $client->setAuthConfigFile($ini['gmail_client_secret_path']); $client->setLoginHint($ini['gmail_username']); // We want access even when the user is not logged in $client->setAccessType('offline'); // Load previously authorized credentials from a file. $credentialsPath = expandHomeDirectory($ini['gmail_credentials_path']); if (file_exists($credentialsPath)) { if (DEBUG) { print "Using existing access token from {$credentialsPath}\n"; } $accessToken = file_get_contents($credentialsPath); } else { // Request authorisation from the user. if (DEBUG) { print "Requesting authorisation\n"; } $authURL = $client->createAuthUrl(); if (php_sapi_name() != 'cli') { // Re-direct browser to authentication URL header('Location: ' . filter_var($authURL, FILTER_SANITIZE_URL)); } print "Open the following link in your browser:\n{$authURL}\n"; print 'Enter verification code: '; $authCode = trim(fgets(STDIN)); // Exchange authorization code for an access token. $accessToken = $client->authenticate($authCode); // Store the credentials to disk. if (!file_exists(dirname($credentialsPath))) { mkdir(dirname($credentialsPath), 0700, true); } file_put_contents($credentialsPath, $accessToken); if ($verbose > 3) { print "Credentials saved to {$credentialsPath}\n"; } } if (DEBUG) { print_r($accessToken); print "\n"; } $client->setAccessToken($accessToken); // Refresh the token if it's expired. if ($client->isAccessTokenExpired()) { $client->refreshToken($client->getRefreshToken()); $accessToken = $client->getAccessToken(); // Check we've not lost the refresh token if (FALSE == strstr($accessToken, 'refresh_token')) { die("Error: Refreshed access token no longer contains refresh token used to retrieve it\n"); } if (DEBUG) { print "Refreshed accesss token:\n"; print_r($accessToken); } file_put_contents($credentialsPath, $accessToken); } } catch (Exception $e) { print 'Exception: ' . $e->getMessage(); } return $client; }
$ini = array('gmail_credentials_path' => getcwd() . '/bec_fault_mon.json', 'gmail_client_secret_path' => getcwd() . '/client_secret.json', 'gmail_username' => 'me'); if (!is_null($_GET['inifilename'])) { $iniFilename = $_GET['inifilename']; } if (file_exists($iniFilename)) { $ini = array_merge($ini, parse_ini_file($iniFilename)); } else { if ($iniFilename != BECFM_INI_FILENAME) { die("<HTML>Error: Requested ini file '{$iniFilename}' not found</HMTL>"); } } // We're expecting a code (_GET['code'] which we need to exchange for an access token require_once 'vendor/autoload.php'; $client = new Google_Client(); $client->setAuthConfigFile($ini['gmail_client_secret_path']); $client->setLoginHint($ini['gmail_username']); if (isset($_GET['code'])) { // Exchange code for access token $client->authenticate($_GET['code']); $token = $client->getAccessToken(); $filename = $ini['gmail_credentials_path']; if (strlen($client->getRefreshToken()) < 5) { /* We've lost the refresh token - it's better to revoke this * token now and re-request access from the user. */ $client->revokeToken(); unlink($filename); } else { // Save the access token if (strlen($token) != file_put_contents($filename, $token)) { die("<HTML>Error: Failed to write access token to {$filename}</HTML>");