简体   繁体   English

Google Calendar API-禁止-服务帐户错误

[英]Google Calendar API - Forbidden - Error with Service Account

I try to use the Google Calendar API by php running server side. 我尝试通过运行服务器端的php使用Google Calendar API。

I created a service account and use its credentials (via the json file). 我创建了一个服务帐户并使用其凭据(通过json文件)。 I am NOT using G Suite. 我没有使用G Suite。

Now I am able to list events of my calendar but I get a Forbidden - Error, when I try to create a new event. 现在,我可以列出日历中的事件,但是当我尝试创建新事件时,出现“禁止-错误”。

I shared my calendar with the service account's email address and gave it admin rights for the calendar. 我与服务帐户的电子邮件地址共享了日历,并授予了该日历的管理员权限。 I don't know what I am doing wrong! 我不知道我在做什么错! I just can't get it working. 我就是无法正常工作。 I am also using the scope Google_Service_Calendar::CALENDAR. 我也在使用范围Google_Service_Calendar :: CALENDAR。

The exception: 403 - Forbidden 例外:403-禁止

I have no idea how to debug this problem and how to proceed further. 我不知道如何调试此问题以及如何继续进行。

Can you help me? 你能帮助我吗?

Edit: here is my calling code: 编辑:这是我的呼叫代码:

putenv('GOOGLE_APPLICATION_CREDENTIALS=../google-service-account.json');

$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes([Google_Service_Calendar::CALENDAR]);
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$service = new Google_Service_Calendar($client);

$event = new Google_Service_Calendar_Event([
  'summary' => $name,
  'location' => $location,
  'description' => $desc,
  'start' => [
    'dateTime' => $start,
    'timeZone' => $timezone,
  ],
  'endTimeUnspecified' => true,
]);

$calendar = $service->calendars->get($calendarId);
$settings = $service->settings->listSettings();
$list = $service->acl->listAcl($calendarId);
$event = $service->events->insert($calendarId, $event);

The first three service calls ( $calender = ... , $settings = ... and $list = ... ) work fine and in the acl I can also see that the email address of my service account has all rights (role: owner). 前三个服务调用( $calender = ...$settings = ...$list = ... )可以正常工作,在acl中,我还可以看到我的服务帐户的电子邮件地址具有所有权利(角色:所有者)。 But the last line $event = ... gives the forbidden error... 但是最后一行$event = ...给出了禁止的错误...

'endTimeUnspecified' => true,

is causing your problem (its googles fault, but you cant tell them that) 是造成您的问题的原因(它是Google的错,但您不能告诉他们)

if you change to 如果您更改为

 'end' => [
'dateTime' => $start2,
'timeZone' => $timezone,
 ],

it will work (i know it is not what you want and maybe you can figure out a way to adjust $start2 to give you what you want as an end time but the endTimeUnspecified is what is causing it to say 403 forbidden 它会工作(我知道这不是您想要的,也许您可​​以想出一种方法来调整$ start2来给您想要的结束时间,但是endTimeUnspecified是导致它禁止使用403的原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM