t3living
t3living

Reputation: 35

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(
    'client_secret'=>'My-Secret',
    'grant_type'=>'refresh_token',
    'approval_promt'=> 'force',
    'refresh_token'=>'My-Refresh-Token',
    'client_id'=>'My-Client-ID'
);
$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'){
  date_default_timezone_set("America/Chicago");
  $client->addScope('https://www.googleapis.com/auth/calendar');
  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(
    'orderBy'=>'starttime',
    'singleEvents'=>False,
    'timeMax'=>$endDate,
    'timeMin'=>$startDate,
    'timeZone'=>'America/Chicago',
    'maxResults'=>1000
  );

  try{
    $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>";
try{
  $client->setAccessToken($accessToken);
}
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);

Upvotes: 2

Views: 3265

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116918

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.

    <?php
session_start();        
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();      
$client->setApplicationName("Client_Library_Examples");
$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(    
    $Email_address,      
    array($scopes),     
    $key         
    );      
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {        
    $client->getAuth()->refreshTokenWithAssertion($cred);       
}       
$service = new Google_Service_Calendar($client);    

?>

<html><body>

<?php
$calendarList  = $service->calendarList->listCalendarList();
print_r($calendarList);
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 {
        break;
    }
}    

?>
</html> 

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

Upvotes: 2

Related Questions