Why google Calendar event not being created with stored access token and refresh token in php

瘦欲@ 提交于 2021-01-05 07:41:36

问题


I have successfully integrated google OAuth 2.0 with Calendar permissions and I am storing access token and refresh token in the database, after that fetch both token. do not know what wrong I am doing the Calendar event not being created.

<?php 
include($_SERVER["DOCUMENT_ROOT"]."/config.php");
include($_SERVER["DOCUMENT_ROOT"]."/functions.php");
require  'vendor/autoload.php';

function getClient($useremail)
{
$client = new Google_Client();
$client->setApplicationName('Google Calendar API PHP Quickstart');
$client->setScopes(Google_Service_Calendar::CALENDAR);
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');

$currentTime=date('Y-m-d H:i:s');
$result = dbRowsSelect(array('refresh_token','access_token','expire_on'), "calendar_api_users", 
 "email = '".$useremail."' ","LIMIT 1");

if(count($result) > 0)
{
      $accessToken = $result[0]['access_token']; 
      $expire_on=$result[0]['expire_on'];
    if(strtotime($currentTime) < strtotime($expire_on))
      {
          $client->setAccessToken($accessToken);
      }
      else
      {
          $refreshToken = $result[0]['refresh_token'];
          // Refresh the token if possible, else fetch a new one.
          $client->fetchAccessTokenWithRefreshToken($refreshToken);

      }

    
}
return $client;
}


// Get the API client and construct the service object.
$email='xxxxx.xxxxxxx@gmail.com';
$client = getClient($email);
$service = new Google_Service_Calendar($client);
$calendarId = 'primary';
$optParams = array(
'maxResults' => 10,
'orderBy' => 'startTime',
'singleEvents' => true,
'timeMin' => date('c'),
 );
 $results = $service->events->listEvents($calendarId, $optParams);
 $events = $results->getItems();

 $event = new Google_Service_Calendar_Event(array(
'summary' => 'Google I/O 2015',
'location' => '800 Howard St., San Francisco, CA 94103',
'description' => 'A chance to hear more about Google\'s developer products.',
'start' => array(
'dateTime' => '2021-06-29T09:00:00-07:00',
'timeZone' => 'America/Los_Angeles',
),
'end' => array(
'dateTime' => '2021-06-29T17:00:00-07:00',
'timeZone' => 'America/Los_Angeles',
),
'recurrence' => array(
'RRULE:FREQ=DAILY;COUNT=1'
),
'reminders' => array(
'useDefault' => FALSE,
'overrides' => array(
  array('method' => 'email', 'minutes' => 24 * 60),
  array('method' => 'popup', 'minutes' => 10),
),
),
));

$calendarId = 'primary';
$event = $service->events->insert($calendarId, $event);
printf('Event created: %s\n', $event->htmlLink);

I have fetched access token and refresh token from database , I dont know where is error as its showing white screen no error.

Thanks in advance


回答1:


When you get the result back from the database, set both the access token and the refresh token.

Then call isAccessTokenExpired if it is then call fetchAccessTokenWithRefreshToken

$client->refreshToken($result[0]['refresh_token']);
$client->setAccessToken($result[0]['access_token']);                    
            
// Refresh the access token if it's expired.
if ($client->isAccessTokenExpired()) {              
        $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
        $client->setAccessToken($client->getAccessToken()); 
        // Note: add code here to save the new access token and refresh token.          
        }   

NOTE: IMO there is no reason for you to be storing the access token you can just store the refresh token set that and then request a new one everytime. Google wont mind. 😊



来源:https://stackoverflow.com/questions/65233736/why-google-calendar-event-not-being-created-with-stored-access-token-and-refresh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!