Google api 谷歌日历API V3删除API
我在尝试使用deleteapi时遇到问题。 我的系统使用Google帐户服务创建多个事件。 当我尝试使用deleteapi删除所有事件时,就会出现问题 cakephp框架中的代码Google api 谷歌日历API V3删除API,google-api,google-calendar-api,google-apis-explorer,Google Api,Google Calendar Api,Google Apis Explorer,我在尝试使用deleteapi时遇到问题。 我的系统使用Google帐户服务创建多个事件。 当我尝试使用deleteapi删除所有事件时,就会出现问题 cakephp框架中的代码 $service->calendars->delete(GOOGLE_CALENDAR_ID); 我检查了日历上的一些事件,它可能会删除一些事件,然后返回一个错误 代码:500, 消息:后端错误 有人能帮我吗 感谢谷歌日历API中的CakePHP 3.x 按照以下步骤操作: 创建谷歌日历api项目 要安
$service->calendars->delete(GOOGLE_CALENDAR_ID);
我检查了日历上的一些事件,它可能会删除一些事件,然后返回一个错误
代码:500,
消息:后端错误
有人能帮我吗
感谢谷歌日历API中的CakePHP 3.x
按照以下步骤操作:
创建谷歌日历api项目
要安装的Composer:
谷歌/apiclient:^2.0
所需的谷歌日历api集成
创建项目并创建密钥和客户端id
设置名称和重定向URL中的项目
注意:-重定向URL必须是.com和.org域
如果您在本地计算机中开发,则创建以下类型的虚拟主机
example.com和example.org
然后创建虚拟主机
按照以下步骤操作:
在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]),
]
]
路线
**
控制器
**
授权google Oauth
/**
*获取谷歌日历客户端
*/
/**
*创建谷歌日历事件
*/
添加事件功能
编辑事件
//事件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);
}
}
有一个问题。放置您的calendarID并执行。感谢您的回复,但我正在使用谷歌帐户服务。您的方法仅适用于已登录的用户。这不是我想要的。
use Google_Client;
use Google_Service_Calendar;
use Google_Service_Calendar_Event;
use Google_Service_Drive;
use Google_Service_Drive_DriveFile;
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);
}
}
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);
}
}