Refresh the access token for Google Calendar API
Currently, I am using https://developers.google.com/calendar/quickstart/php this api but token expire after 3-4 hours, how to auto refresh access token for Google Calendar API
Current Code is:
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Google Calendar API PHP Quickstart');
$client->setScopes(Google_Service_Calendar::CALENDAR);
$client->setAuthConfig(__DIR__ . '/google-calender/credentials.json');
$client->setAccessType('offline');
$client ->setApprovalPrompt ('force');
// Load previously authorized token from a file, if it exists.
$tokenPath =__DIR__ . '/google-calender/token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
}
// If there is no previous token or it's expired.
if ($client->isAccessTokenExpired()) {
// Refresh the token if possible, else fetch a new one.
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:n%sn", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
// Check to see if there was an error.
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
}
// Save the token to a file.
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 0700, true);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
return $client; }
php google-api google-calendar-api google-oauth google-api-php-client
add a comment |
Currently, I am using https://developers.google.com/calendar/quickstart/php this api but token expire after 3-4 hours, how to auto refresh access token for Google Calendar API
Current Code is:
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Google Calendar API PHP Quickstart');
$client->setScopes(Google_Service_Calendar::CALENDAR);
$client->setAuthConfig(__DIR__ . '/google-calender/credentials.json');
$client->setAccessType('offline');
$client ->setApprovalPrompt ('force');
// Load previously authorized token from a file, if it exists.
$tokenPath =__DIR__ . '/google-calender/token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
}
// If there is no previous token or it's expired.
if ($client->isAccessTokenExpired()) {
// Refresh the token if possible, else fetch a new one.
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:n%sn", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
// Check to see if there was an error.
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
}
// Save the token to a file.
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 0700, true);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
return $client; }
php google-api google-calendar-api google-oauth google-api-php-client
add a comment |
Currently, I am using https://developers.google.com/calendar/quickstart/php this api but token expire after 3-4 hours, how to auto refresh access token for Google Calendar API
Current Code is:
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Google Calendar API PHP Quickstart');
$client->setScopes(Google_Service_Calendar::CALENDAR);
$client->setAuthConfig(__DIR__ . '/google-calender/credentials.json');
$client->setAccessType('offline');
$client ->setApprovalPrompt ('force');
// Load previously authorized token from a file, if it exists.
$tokenPath =__DIR__ . '/google-calender/token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
}
// If there is no previous token or it's expired.
if ($client->isAccessTokenExpired()) {
// Refresh the token if possible, else fetch a new one.
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:n%sn", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
// Check to see if there was an error.
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
}
// Save the token to a file.
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 0700, true);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
return $client; }
php google-api google-calendar-api google-oauth google-api-php-client
Currently, I am using https://developers.google.com/calendar/quickstart/php this api but token expire after 3-4 hours, how to auto refresh access token for Google Calendar API
Current Code is:
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Google Calendar API PHP Quickstart');
$client->setScopes(Google_Service_Calendar::CALENDAR);
$client->setAuthConfig(__DIR__ . '/google-calender/credentials.json');
$client->setAccessType('offline');
$client ->setApprovalPrompt ('force');
// Load previously authorized token from a file, if it exists.
$tokenPath =__DIR__ . '/google-calender/token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
}
// If there is no previous token or it's expired.
if ($client->isAccessTokenExpired()) {
// Refresh the token if possible, else fetch a new one.
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:n%sn", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
// Check to see if there was an error.
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
}
// Save the token to a file.
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 0700, true);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
return $client; }
php google-api google-calendar-api google-oauth google-api-php-client
php google-api google-calendar-api google-oauth google-api-php-client
edited Nov 21 '18 at 11:20


DaImTo
44.5k1160239
44.5k1160239
asked Nov 21 '18 at 11:09


Wordpress DeveloperWordpress Developer
114
114
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You need to set the new access token on your client after requesting a new one.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
$client->setAccessToken($client->getAccessToken());
$_SESSION['access_token'] = $client->getAccessToken();
}
my sample
client->getAccessToken() return this value: {"access_token":"ya29.GltbBgaJz6eYMpLwdqrwe2MmT-yu3sVt539OnKpu1kiy-tNtJ14rSEmopHxKW7_vJ5uZ2zHPKofki4msLFaXtWxqrveKRrS-GD7Th4taKteX1Q9A4f9UYVzDih-4","expires_in":3600,"scope":"https://www.googleapis.com/auth/calendar","token_type":"Bearer","created":1542802697} access_token is new refresh token?
– Wordpress Developer
Nov 21 '18 at 12:50
No the access token is the access token you use to access the API. Its only valid for an hour (3600) Refresh tokens dont expire as long as you have it saved some where you will be able to use it to access the users data by requesting a new access token when ever you need.
– DaImTo
Nov 21 '18 at 13:45
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53410832%2frefresh-the-access-token-for-google-calendar-api%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to set the new access token on your client after requesting a new one.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
$client->setAccessToken($client->getAccessToken());
$_SESSION['access_token'] = $client->getAccessToken();
}
my sample
client->getAccessToken() return this value: {"access_token":"ya29.GltbBgaJz6eYMpLwdqrwe2MmT-yu3sVt539OnKpu1kiy-tNtJ14rSEmopHxKW7_vJ5uZ2zHPKofki4msLFaXtWxqrveKRrS-GD7Th4taKteX1Q9A4f9UYVzDih-4","expires_in":3600,"scope":"https://www.googleapis.com/auth/calendar","token_type":"Bearer","created":1542802697} access_token is new refresh token?
– Wordpress Developer
Nov 21 '18 at 12:50
No the access token is the access token you use to access the API. Its only valid for an hour (3600) Refresh tokens dont expire as long as you have it saved some where you will be able to use it to access the users data by requesting a new access token when ever you need.
– DaImTo
Nov 21 '18 at 13:45
add a comment |
You need to set the new access token on your client after requesting a new one.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
$client->setAccessToken($client->getAccessToken());
$_SESSION['access_token'] = $client->getAccessToken();
}
my sample
client->getAccessToken() return this value: {"access_token":"ya29.GltbBgaJz6eYMpLwdqrwe2MmT-yu3sVt539OnKpu1kiy-tNtJ14rSEmopHxKW7_vJ5uZ2zHPKofki4msLFaXtWxqrveKRrS-GD7Th4taKteX1Q9A4f9UYVzDih-4","expires_in":3600,"scope":"https://www.googleapis.com/auth/calendar","token_type":"Bearer","created":1542802697} access_token is new refresh token?
– Wordpress Developer
Nov 21 '18 at 12:50
No the access token is the access token you use to access the API. Its only valid for an hour (3600) Refresh tokens dont expire as long as you have it saved some where you will be able to use it to access the users data by requesting a new access token when ever you need.
– DaImTo
Nov 21 '18 at 13:45
add a comment |
You need to set the new access token on your client after requesting a new one.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
$client->setAccessToken($client->getAccessToken());
$_SESSION['access_token'] = $client->getAccessToken();
}
my sample
You need to set the new access token on your client after requesting a new one.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
$client->setAccessToken($client->getAccessToken());
$_SESSION['access_token'] = $client->getAccessToken();
}
my sample
answered Nov 21 '18 at 11:19


DaImToDaImTo
44.5k1160239
44.5k1160239
client->getAccessToken() return this value: {"access_token":"ya29.GltbBgaJz6eYMpLwdqrwe2MmT-yu3sVt539OnKpu1kiy-tNtJ14rSEmopHxKW7_vJ5uZ2zHPKofki4msLFaXtWxqrveKRrS-GD7Th4taKteX1Q9A4f9UYVzDih-4","expires_in":3600,"scope":"https://www.googleapis.com/auth/calendar","token_type":"Bearer","created":1542802697} access_token is new refresh token?
– Wordpress Developer
Nov 21 '18 at 12:50
No the access token is the access token you use to access the API. Its only valid for an hour (3600) Refresh tokens dont expire as long as you have it saved some where you will be able to use it to access the users data by requesting a new access token when ever you need.
– DaImTo
Nov 21 '18 at 13:45
add a comment |
client->getAccessToken() return this value: {"access_token":"ya29.GltbBgaJz6eYMpLwdqrwe2MmT-yu3sVt539OnKpu1kiy-tNtJ14rSEmopHxKW7_vJ5uZ2zHPKofki4msLFaXtWxqrveKRrS-GD7Th4taKteX1Q9A4f9UYVzDih-4","expires_in":3600,"scope":"https://www.googleapis.com/auth/calendar","token_type":"Bearer","created":1542802697} access_token is new refresh token?
– Wordpress Developer
Nov 21 '18 at 12:50
No the access token is the access token you use to access the API. Its only valid for an hour (3600) Refresh tokens dont expire as long as you have it saved some where you will be able to use it to access the users data by requesting a new access token when ever you need.
– DaImTo
Nov 21 '18 at 13:45
client->getAccessToken() return this value: {"access_token":"ya29.GltbBgaJz6eYMpLwdqrwe2MmT-yu3sVt539OnKpu1kiy-tNtJ14rSEmopHxKW7_vJ5uZ2zHPKofki4msLFaXtWxqrveKRrS-GD7Th4taKteX1Q9A4f9UYVzDih-4","expires_in":3600,"scope":"https://www.googleapis.com/auth/calendar","token_type":"Bearer","created":1542802697} access_token is new refresh token?
– Wordpress Developer
Nov 21 '18 at 12:50
client->getAccessToken() return this value: {"access_token":"ya29.GltbBgaJz6eYMpLwdqrwe2MmT-yu3sVt539OnKpu1kiy-tNtJ14rSEmopHxKW7_vJ5uZ2zHPKofki4msLFaXtWxqrveKRrS-GD7Th4taKteX1Q9A4f9UYVzDih-4","expires_in":3600,"scope":"https://www.googleapis.com/auth/calendar","token_type":"Bearer","created":1542802697} access_token is new refresh token?
– Wordpress Developer
Nov 21 '18 at 12:50
No the access token is the access token you use to access the API. Its only valid for an hour (3600) Refresh tokens dont expire as long as you have it saved some where you will be able to use it to access the users data by requesting a new access token when ever you need.
– DaImTo
Nov 21 '18 at 13:45
No the access token is the access token you use to access the API. Its only valid for an hour (3600) Refresh tokens dont expire as long as you have it saved some where you will be able to use it to access the users data by requesting a new access token when ever you need.
– DaImTo
Nov 21 '18 at 13:45
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53410832%2frefresh-the-access-token-for-google-calendar-api%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown