doulun1939 2017-06-03 17:44
浏览 76
已采纳

Google Calendar API - 禁止访问 - 服务帐户出错

I try to use the Google Calendar API by php running server side.

I created a service account and use its credentials (via the json file). I am NOT using 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.

The exception: 403 - Forbidden

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). But the last line $event = ... gives the forbidden error...

  • 写回答

1条回答 默认 最新

  • doujizhong8352 2017-06-13 21:19
    关注
    'endTimeUnspecified' => true,
    

    is causing your problem (its googles fault, but you cant tell them that)

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?