2017-09-25 263 views
0

嘗試使用刪除API(The documentation from Google)時遇到問題。 我的系統使用Google帳戶服務創建多個活動。 當我嘗試使用刪除API刪除所有事件時發生問題 cakephp框架中的代碼。Google Calendar API V3刪除API

$service->calendars->delete(GOOGLE_CALENDAR_ID); 

我查了一下日曆中的某些事件,它可以刪除一些事件,然後返回一個錯誤

「代碼」:500, 「消息」: 「後端錯誤」

任何人都可以幫助我?

感謝

+0

有一個[試試,它部分(https://developers.google.com/google-apps/calendar/ V3 /參考/日曆/清除#試穿吧)。放置您的日曆ID並執行。 – noogui

+0

感謝您的回覆,但我正在使用Google帳戶服務。您的方法僅適用於登錄用戶。這不是我想要的。 –

回答

0

CakePHP的3.X在谷歌日曆API

設置配置文件中app_globle。PHP

[ 
'Google' => [ 
     'ClientID' => '7441260037.apps.googleusercontent.com', 
     'ClientSecret' => 'kashdjahdkjshdkjhjAs', 
     'RedirectUrl' => 'http://' . env("HTTP_HOST") . '/oauth2calendars', 
     'ClientCredentials' => WWW_ROOT . 'files'. DS.'google.txt', 
     'Scopes' => implode(' ', [Google_Service_Calendar::CALENDAR, Google_Service_Drive::DRIVE, Google_Service_Drive::DRIVE_FILE, Google_Service_Drive::DRIVE_APPDATA, Google_Service_Drive::DRIVE_METADATA]), 
    ] 
] 

路線

$routes->connect('/events/add', ['controller' => 'Events', 'action' => 'add', ['_name' => 'add-event']); 

    $routes->connect('/events/edit/:id', ['controller' => 'Events', 'action' => 'edit', ['id' => '\d+', 'pass' => ['id'], '_name' => 'edit-event']); 

    $routes->connect('/events/delete/:id', ['controller' => 'Events', 'action' => 'delete', ['id' => '\d+', 'pass' => ['id'], '_name' => 'delete-event']); 

**

  • 控制器

**

use Google_Client; 
use Google_Service_Calendar; 
use Google_Service_Calendar_Event; 
use Google_Service_Drive; 
use Google_Service_Drive_DriveFile; 

授權的谷歌的Oauth /** * 得到谷歌日曆客戶端 */

private function getClient() 
    { 
     $client = new Google_Client(); 
     $client->setAccessType("offline"); 
     $client->setClientId(Configure::read('Google.ClientID')); 
     $client->setClientSecret(Configure::read('Google.ClientSecret')); 
     $client->setRedirectUri(Configure::read('Google.RedirectUrl')); 
     $client->setScopes(Configure::read('Google.Scopes')); 
     $credentialsPath = Configure::read('Google.Credentials'); 
     if (file_exists($credentialsPath)) { 
      $accessToken = json_decode(file_get_contents($credentialsPath), true); 
     } else { 
      // Request authorization from the user. 
      $authUrl = $client->createAuthUrl(); 
      return $this->redirect($authUrl); 
     } 
     $client->setAccessToken($accessToken); 
     // Refresh the token if it's expired. 
     if ($client->isAccessTokenExpired()) { 
      $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken()); 
      file_put_contents($credentialsPath, json_encode($client->getAccessToken())); 
     } 
     return $client; 
    } 

/** * 創建谷歌日曆事件 */

public function createCredentials() 
    { 
     $client = new Google_Client(); 
     $client->setAccessType("offline"); 
     $client->setClientId(Configure::read('Google.ClientID')); 
     $client->setClientSecret(Configure::read('Google.ClientSecret')); 
     $client->setRedirectUri(Configure::read('Google.RedirectUrl')); 
     $client->setScopes(Configure::read('Google.Scopes')); 

       if (isset($this->request->query['code'])) { 
      $client->authenticate($this->request->query['code']); 
      $token = json_encode($client->getAccessToken()); 
      $credentialsPath =WWW_ROOT . 'files'. DS.'google.txt'; 
      if (!file_exists(dirname($credentialsPath))) { 
       mkdir(dirname($credentialsPath), 0700, true); 
      } 
      $file = new File($credentialsPath, true); 
      $file->write($token); 
      $client->setAccessToken($token); 
      return $this->redirect(‘/add-event’); 
     } 
    } 

添加活動功能

Public function add() 
{ 
$client = $this->getClient(); 
if ($this->request->is('post')) { 
       $dateStart = new \DateTime($this->request->data['start_date_time'], new \DateTimeZone('Asia/Kolkata')); 
       $dateStart->setTimezone(new \DateTimeZone('UTC')); 
       $startDate = $dateStart->format('Y-m-d H:i:s'); 
       $dateEnd = new \DateTime($this->request->data['end_date_time'], new \DateTimeZone('Asia/Kolkata')); 
       $dateEnd->setTimezone(new \DateTimeZone('UTC')); 
       $endDate = $dateEnd->format('Y-m-d H:i:s'); 
       $guests = $this->request->data['guests']; 
       $eventGuest = []; 
       $service = new Google_Service_Calendar($client); 
       foreach ($guests as $key => $value) { 
        $eventGuest[$key]['email'] = $value; 
       } 
       $eventData = [ 
        'summary' => $this->request->data['name'], 
        'location' => $this->request->data['location'], 
        'description' => $this->request->data['description'], 
        'start' => [ 
         'dateTime' => date("c", strtotime($startDate)), 
        ], 
        'end' => [ 
         'dateTime' => date("c", strtotime($endDate)), 
        ], 
        'attendees' => $eventGuest, 
        'reminders' => [ 
         'useDefault' => true, 
        ], 
        'visibility' => 'private', 
        'privateCopy' => true, 
       ]; 
       $event = new Google_Service_Calendar_Event($eventData); 
       $sendNotifications = ['sendNotifications' => true]; 
       $calendarId = 'primary'; 
       $eventDataResponse = $service->events->insert($calendarId, $event, $sendNotifications); 
      } 

} 

編輯活動 //事件ID $ ID

Public function edit($id) 
{ 
$client = $this->getClient(); 
if ($this->request->is(['patch', 'post', 'put'])) { 
       $service = new Google_Service_Calendar($client); 
       if (isset($eventID) && !empty($eventID)) { 
        $eventData = $service->events->get('primary', $eventID); 
       } 
       $guests = $this->request->data['guests']; 
       $eventGuest = []; 
       foreach ($guests as $key => $value) { 
        $eventGuest[$key]['email'] = $value; 
       } 
       $eventData->setSummary($this->request->data['name']); 
       $eventData->setDescription($this->request->data['description']); 
       $eventData->setLocation($this->request->data['location']); 
       $eventData->setAttendees($eventGuest); 
       $sendNotifications = ['sendNotifications' => true]; 
       $updatedEvent = $service->events->update('primary', $eventData->getId(), $eventData, $sendNotifications); 
              $this->Flash->error(__('Event could not be update. Please, try again.')); 
      } 
} 
Deleted event 
//event id $id 
// if google calendar in event is exist otherwise not to dispaly 
Public function delete($id) 
{ 
$client = $this->getClient(); 
      if ($this->request->is(['post', 'put', 'patch'])) { 
$service = new Google_Service_Calendar($client); 
       $sendNotifications = ['sendNotifications' => true]; 
       $service->events->delete('primary', $eventID, $sendNotifications); 
    } 
}