I have been working through the docs, and so far, I have managed to add a new event to my calendar, using my authorization credentials from the Google Developers Console and the code snippet here
https://developers.google.com/google-apps/calendar/v3/reference/events/insert
<?php
$client = new Google_Client();
// OAuth2 client ID and secret can be found in the Google Developers Console.
$client->setClientId('xxxxx.apps.googleusercontent.com');
$client->setClientSecret(xxxxx);
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$client->addScope('https://www.googleapis.com/auth/calendar');
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$service = new Google_Service_Calendar($client);
$authUrl = $client->createAuthUrl();
//Request authorization
print "Please visit:
$authUrl
";
print "Please enter the auth code:
";
//$authCode = trim(fgets(STDIN));
$authCode = '';
// Exchange authorization code for access token
$accessToken = $client->authenticate($authCode);
$client->setAccessToken($accessToken);
$event = new Google_Service_Calendar_Event();
$event->setSummary('Appointment);
$event->setLocation('Somewhere');
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2014-12-22T10:00:00.000-07:00');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2014-12-22T10:00:00.000-08:00');
$event->setEnd($end);
$attendee1 = new Google_Service_Calendar_EventAttendee();
$attendee1->setEmail(xxx@gmail.com');
// ...
$attendees = array($attendee1);
$event->attendees = $attendees;
$createdEvent = $service->events->insert('xxxxx@group.calendar.google.com', $event);
echo $createdEvent->getId();
?>
This works, but I need to get a new OAuth2.0 access token every time. What I want to do is use a refresh token to automatically generate a new access token, so I can add new gcal events from my web page
If I run the following code, I can successfully regenerate a new access token using the refresh token
<?php
function getAccessToken(){
$tokenURL = 'https://accounts.google.com/o/oauth2/token';
$postData = array(
'client_secret'=>'xxxxx',
'grant_type'=>'refresh_token',
'refresh_token'=>'xxxxx',
'client_id'=>'xxxx.apps.googleusercontent.com'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $tokenURL);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//need this otherwise you get an ssl error
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tokenReturn = curl_exec($ch);
$token = json_decode($tokenReturn);
//var_dump($tokenReturn);
$accessToken = $token->access_token;
return ($accessToken);
}
?>
This always gives me a new access token, but if I put this token into the code at the top of the page, I get an error. Google_Auth_Exception' with message 'The OAuth 2.0 access token has expired, and a refresh token is not available
I have tried returning the access token only, the return value, the json decoded return value, but I always get an error of some sort
Can anyone point me in the right direction, or to a code snippet which will allow me to insert a new event into my calendar ?
Thanks
Thanks for the link SGC
I think the reason for the initial failure, is that the function to create a new access token using the refresh token only returns 3 values, the 'access_token' itself, the 'token_type', and 'expires_in'.
What is also required when calling setAccessToken() is the 'refresh_token' and the 'created' value.
I had the refresh token already, but I am not clear on the 'created', is it when the refresh token was created or the access token, and will this value change ?