I am using the Google API Client to access Google Analytics. I want to access the data in offline mode, so I need a refresh token. How do I get a refresh_token?
To obtain this access token, we must redirect the user on a page hosted by Google so that the user can log on (to the Google site, not to our own) and authorize our application to access the desired service (Google Calendar in our example).
To use the refresh token, make a POST request to the service's token endpoint with grant_type=refresh_token , and include the refresh token as well as the client credentials if required.
Now go to Variables tab in this collection and paste your client Id and secret. Also enter appropriate scope. In Authorization tab of collection, just hit "Get New Access Token". That will open up a web browser window asking for your authentication.
try using the following code:
<?php
require_once 'apiClient.php';
const REDIRECT_URL = 'INSERT YOUR REDIRECT URL HERE';
const CLIENT_ID = 'INSERT YOUR CLIENT ID HERE';
const CLIENT_SECRET = 'INSERT YOUR CLIENT SECRET';
const ANALYTICS_SCOPE = 'https://www.googleapis.com/auth/analytics.readonly';
// Build a new client object to work with authorization.
$client = new apiClient();
$client->setClientId(CLIENT_ID);
$client->setClientSecret(CLIENT_SECRET);
$client->setRedirectUri(REDIRECT_URL);
$client->setScopes(array(ANALYTICS_SCOPE));
$client->setAccessType('offline');
$auth = $client->authenticate();
if ($client->getAccessToken()) {
$token = $client->getAccessToken();
$authObj = json_decode($token);
$refreshToken = $authObj->refresh_token;
}
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With