t3living t3living - 1 year ago 246
PHP Question

Google Calendar API using refresh token

I have a php app on my site that allows my users to schedule/reschedule calendar events using one of my google calendars, so I don't need the users to authenticate themselves with google. I have worked through getting a token and stored a refresh token but now when I try to access calendar I get an error that says my token has expired. The output is

creating a client
found access token = { "access_token" : "long...token...string", "token_type" : "Bearer", "expires_in" : 3600 }
An error occurred: (0) The OAuth 2.0 access token has expired, and a refresh token is not available. Refresh tokens are not returned for responses that were auto-approved.

Not sure why I'm getting this error.

function getAccessToken(){
$tokenURL = 'https://accounts.google.com/o/oauth2/token';
$postData = array(
'approval_promt'=> 'force',
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $tokenURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$tokenReturn = curl_exec($ch);
return $tokenReturn;

function outputCalendarByDateRange($client, $startDate='2007-05-01', $endDate='2007-08-01'){
try {
$service = new Google_Service_Calendar($client);
}catch(Google_ServiceException $e){
print "Error code :" . $e->getCode() . "\n";
print "Error message: " . $e->getMessage() . "\n";
} catch (Google_Exception $e) {
print "An error occurred: (" . $e->getCode() . ") " . $e->getMessage() . "\n";

$optParams = array(

$events = $service->events->listEvents('primary',$optParams);
} catch (Google_ServiceException $e) {
print "Error code :" . $e->getCode() . "\n";
print "Error message: " . $e->getMessage() . "\n";
} catch (Google_Exception $e) {
print "An error occurred: (" . $e->getCode() . ") " . $e->getMessage() . "\n";

foreach ($events->getItems() as $event) {
echo $event->getSummary();

echo "creating a client<br>";
$client = new Google_Client();
$accessToken = getAccessToken();
echo "found access token = ".$accessToken."<br>";
catch (Google_ServiceException $e) {
print "Error code :" . $e->getCode() . "\n";
print "Error message: " . $e->getMessage() . "\n";
} catch (Google_Exception $e) {
print "An error occurred: (" . $e->getCode() . ") " . $e->getMessage() . "\n";
outputCalendarByDateRange($client, $today, $tomorrow);

Answer Source

You should consider doing this with a service account. A service account will allow your application to access your Google Calendar data without prompting a user for access.

When you create the service account take the email address it gives you and add it like you would any other user to your Google Calendar. The script will then have access to it.

require_once 'Google/Client.php';
require_once 'Google/Service/Calendar.php';     
 The following 3 values an befound in the setting   
 for the application you created on Google      
 Developers console.         Developers console.
 The Key file should be placed in a location     
 that is not accessable from the web. outside of 
 web root.   

 In order to access your GA account you must    
 Add the Email address as a user at the     
 ACCOUNT Level in the GA admin.         
$client_id = '1046123799103-nk421gjc2v8mlr2qnmmqaak04ntb1dbp.apps.googleusercontent.com';
$Email_address = '1046123799103-nk421gjc2v8mlr2qnmmqaak04ntb1dbp@developer.gserviceaccount.com';     
$key_file_location = '629751513db09cd21a941399389f33e5abd633c9-privatekey.p12';     
$client = new Google_Client();      
$key = file_get_contents($key_file_location);    
// seproate additional scopes with a comma   
$scopes ="https://www.googleapis.com/auth/calendar.readonly";   
$cred = new Google_Auth_AssertionCredentials(    
if($client->getAuth()->isAccessTokenExpired()) {        
$service = new Google_Service_Calendar($client);    



$calendarList  = $service->calendarList->listCalendarList();
while(true) {
    foreach ($calendarList->getItems() as $calendarListEntry) {
        echo "<a href='Oauth2.php?type=event&id=".$calendarListEntry->id." '>".$calendarListEntry->getSummary()."</a><br>\n";
    $pageToken = $calendarList->getNextPageToken();
    if ($pageToken) {
        $optParams = array('pageToken' => $pageToken);
        $calendarList = $service->calendarList->listCalendarList($optParams);
    } else {


Code ripped from tutorial Google Calendar API with PHP – Service Account

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download