连接Google Calendar API使用php获取空数组结果 [英] Connect Google Calendar API get empty array result with php

查看:111
本文介绍了连接Google Calendar API使用php获取空数组结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP和Google的官方PHP客户端库读取Google日历事件.

I'm trying to read my Google Calendar events using PHP and Googles official PHP client library.

我在 Google Developers Console 中创建了service account要工作,我可以连接到日历,但是会得到一个空数组:

I created a service account in the Google Developers Console and it seemed to work, I can connect to the calendar but I get an empty array:

Google_Service_Calendar_CalendarList对象( [collection_key:受保护] =>项目[internal_gapi_mappings:受保护] =>数组()[etag] => [itemsType:protected] => Google_Service_Calendar_CalendarListEntry [itemsDataType:protected] => 数组[种类] => [nextPageToken] => [nextSyncToken] => [modelData:protected] =>数组([错误] =>数组([错误] =>数组 ([0] =>数组([domain] =>全局[reason] => fullyPermissions [message] =>权限不足)) [代码] => 403 [消息] =>权限不足)) [已处理:受保护] =>数组())

Google_Service_Calendar_CalendarList Object ( [collection_key:protected] => items [internal_gapi_mappings:protected] => Array ( ) [etag] => [itemsType:protected] => Google_Service_Calendar_CalendarListEntry [itemsDataType:protected] => array [kind] => [nextPageToken] => [nextSyncToken] => [modelData:protected] => Array ( [error] => Array ( [errors] => Array ( [0] => Array ( [domain] => global [reason] => insufficientPermissions [message] => Insufficient Permission ) ) [code] => 403 [message] => Insufficient Permission ) ) [processed:protected] => Array ( ) )

我的代码:

define('APPLICATION_NAME', 'Google Calendar API PHP Quickstart');
session_start();
$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setAuthConfigFile('client_secret.json');
$client->setAccessType('offline');
$client->setScopes(Google_Service_Calendar::CALENDAR_READONLY);
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
        $client->setAccessToken($_SESSION['access_token']);
        #$client->setUseObjects(true);
        $cal=new Google_Service_Calendar($client);
        $calendarList=$cal->calendarList->listCalendarList();
        while(true) {
                foreach ($calendarList->getItems() as $calendarListEntry) {
                        echo $calendarListEntry;
                }
                $pageToken = $calendarList->getNextPageToken();
                if ($pageToken) {
                        $optParams = array('pageToken' => $pageToken);
                        $calendarList = $service->calendarList->listCalendarList($optParams);
                } else {
                        break;
                }
        }
} else {
        $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/google/oauth2callback.php';
        header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}   

有人可以告诉我我做错了什么吗 在此先感谢您的帮助.

Can anybody tell me what I do wrong Thanks in advance for your help.

推荐答案

您正在使用服务帐户进行连接.服务帐户拥有自己的Google日历帐户,该帐户默认情况下没有任何日历,并且默认情况下无权访问您的Google日历帐户.

You are connecting with a Service account. A service account has its own Google Calendar account which by default does not have any Calendars, and does not by default have access to your Google calendar account.

以下是告诉您的信息:

权限不足

如果您想授予服务帐户访问Google日历的权限,则需要获取服务帐户的电子邮件地址并与其共享Google日历,就像您要与其他任何用户共享该电子邮件地址一样.

If you want to grant the service account access to your Google Calendar you will need to take the service account email address and share your Google calendar with it like you would share it with any other user.

这篇关于连接Google Calendar API使用php获取空数组结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆