For PHP SDK client Google v2:
php composer.phar require google/apiclient:^2.0
You can try the PHP code to update accessToken as follows:
<?php require_once __DIR__ . '/vendor/autoload.php'; define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json'); define('SCOPES', implode(' ', array( Google_Service_Analytics::ANALYTICS_READONLY) )); $client = new Google_Client(); $client->setApplicationName('YOUR APPLICATION NAME'); $client->setScopes(SCOPES); $client->setAuthConfig(CLIENT_SECRET_PATH); $client->setAccessType('offline'); // Input AccessToken from such as session or database $accessToken = json_decode('YOUR CURRENT ACEESS TOKEN'); $client->setAccessToken($accessToken); // Refresh the token if it expired. if ($client->isAccessTokenExpired()) { $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); // Get RefreshToken as json which can be reused $refreshToken = json_encode($client->getAccessToken()); }
Offline issue
In addition, if you want to preserve the existing behavior in your server applications, you need to set approval_prompt to force , otherwise you can get NULL returned from getRefreshToken (), which does not contain a Google document saying:
$client->setApprovalPrompt('force');
Link Upcoming OAuth 2.0 Endpoint Changes
Nick tsai
source share