<?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:\n$authUrl\n\n";
print "Please enter the auth code:\n";
//$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(x...@gmail.com');
// ...
$attendees = array($attendee1);
$event->attendees = $attendees;
$createdEvent = $service->events->insert('xx...@group.calendar.google.com', $event);
echo $createdEvent->getId();
?> <?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);
}
?>');
$attendee1->setEmail(xxx@gmail.com');
// ...
$attendees = array($attendee1);
$event->attendees = $attendees;
$createdEvent = $service->events->insert('xxxxx...@group.calendar.google.com', $event);
echo $createdEvent->getId();
?>
Hi Craig,have you tried using $client->refreshToken($refreshToken) to obtain the refresh token?
Thanks for the link
What is also required when calling setAccessToken() is the 'refresh_token' and the 'created' value.
using the answer here https://stackoverflow.com/questions/25525471/google-oauth-2-0-refresh-token-for-web-application-with-public-access as a guide,
I managed to pass the all of the parameters into setAccessToken()
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 ?
I tried this over the last couple of days, - i stored the 'refresh_token' and 'created' values in my database and build the string with the 'access-token', 'token-type' and 'expires_in'