【发布时间】:2017-10-15 18:54:32
【问题描述】:
我正在尝试使用 Google Calendar API PHP 插入一个事件。我没有错误,我有 $event->hmtlLink,但是当我继续时,谷歌说我“这个事件不存在”。
这是我获取客户端和服务的代码:
putenv("GOOGLE_APPLICATION_CREDENTIALS=" . __DIR__ . "/private/credentials.json");
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes(SCOPES);
$service = new Google_Service_Calendar($client);
$calendarId = "primary";
这是我用来创建事件的函数:
function createEvent($eventId, $name, $equipe, $location, $description, $startTimeDate, $endTimeDate, $timeZone){
$startTimeDate = new DateTime($startTimeDate);
$startTimeDate = date_format($startTimeDate, DATE_ISO8601);
$endTimeDate = new DateTime($endTimeDate);
$endTimeDate = date_format($endTimeDate, DATE_ISO8601);
$event = new Google_Service_Calendar_Event(array(
'id' => $eventId,
'summary' => join('',array("[",$equipe,"] ",$name)),
'location' => $location,
'description' => $description,
'start' => array(
'dateTime' => $startTimeDate,
'timeZone' => $timeZone,
),
'end' => array(
'dateTime' => $endTimeDate,
'timeZone' => $timeZone,
),
));
global $service, $calendarId;
$event = $service->events->insert($calendarId, $event);
printf($event->htmlLink);
printf($event->status);
printf($event->created);
}
第一个 printf 给我发了一个很好的链接,但在打开时:事件不存在
第二次发送“确认”
第三次发送好日期时间
当我尝试插入具有相同 ID 的事件时,我收到一条错误消息,提示此 ID 已存在。但是日历上什么都没有……
请帮帮我! 谢谢。
【问题讨论】:
-
这里只是一个简单的猜测,但请确认您使用的是用于创建活动的同一帐户(在您的凭据文件中)来查看 html 链接 - 如果您有另一个 Google 帐户它可能是默认出现的,而您没有正确的权限来查看添加的事件。
-
我从未使用其他帐户开发过任何东西,而且在我的凭据中,
client_email是好的,所以我认为它是好的帐户。 -
如果您添加事件的凭据是您的电子邮件地址,请尝试使用这些凭据手动登录日历,看看您是否可以在不使用直接访问的情况下查看该日历以及附加到它的任何事件关联。此外,请查看此答案以了解如何从 API 获取直接链接。您可以使用它来确认您的 htmlLink 返回相同的值:stackoverflow.com/a/23469684/463935
标签: php events insert google-calendar-api