为了账号安全,请及时绑定邮箱和手机立即绑定

无法使用 google calendar api php 创建事件

无法使用 google calendar api php 创建事件

PHP
冉冉说 2023-04-28 16:35:10
我已经按照所有说明、php quickstart 和 events.insert 页面进行了操作;但是当我运行它时,会弹出同意书,我单击允许,然后除了同意书重置之外没有任何反应。如果我将重定向 URL 更改为另一个页面,则它不再重置同意书,但仍然没有任何反应。$client = new Google_Client();$client->setAuthConfig('redacted');$client->addScope("https://www.googleapis.com/auth/calendar");$client->addScope("https://www.googleapis.com/auth/calendar.events");$client->setRedirectUri('http://redacted/GoogleClientWorksCalendar.php');//this is the current file$client->setAccessType('offline');$client->setIncludeGrantedScopes(true);$client->setPrompt('consent');$auth_url = $client->createAuthUrl();header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));$service = new Google_Service_Calendar($client);$event = new Google_Service_Calendar_Event(array(  'summary' => 'test',  'location' => 'somewhere',  'description' => 'test description',  'start' => array(    'dateTime' => '2020-09-03T09:00:00+02:00',  ),  'end' => array(    'dateTime' => '2020-09-03T17:00:00+02:00',  ),));$calendarId = 'redacted';$results = $service->events->insert($calendarId, $event);
查看完整描述

1 回答

?
慕沐林林

TA贡献2016条经验 获得超9个赞

我已经解决了我的问题。问题是我忘记了所需的谷歌 Oauth2.0 代码的一部分,这意味着我从未收到访问令牌。下面的代码片段功能齐全。希望对大家有帮助,谢谢大家的回答。


$client = new Google_Client();


$client->setAuthConfig('redacted');


$client->addScope("https://www.googleapis.com/auth/calendar");


$client->addScope("https://www.googleapis.com/auth/calendar.events");


$client->setRedirectUri('http://redacted/GoogleClientWorksCalendar.php');//this is the current file


$client->setAccessType('offline');


$client->setIncludeGrantedScopes(true);


$client->setPrompt('consent');


$auth_url = $client->createAuthUrl();

header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));


$client->authenticate($_GET['code']);

$access_token = $client->getAccessToken();

$client->setAccessToken($access_token);


$service = new Google_Service_Calendar($client);


$event = new Google_Service_Calendar_Event(array(

  'summary' => 'test',

  'location' => 'somewhere',

  'description' => 'test description',

  'start' => array(

    'dateTime' => '2020-09-03T09:00:00+02:00',


  ),

  'end' => array(

    'dateTime' => '2020-09-03T17:00:00+02:00',


  ),




));


$calendarId = 'redacted';

$results = $service->events->insert($calendarId, $event);


查看完整回答
反对 回复 2023-04-28
  • 1 回答
  • 0 关注
  • 64 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信