从Google服务帐户编辑Google日历活动:403

时间:2012-08-08 10:55:12

标签: php google-api google-calendar-api

我想在服务帐户的Google日历中创建新活动。 我可以正确访问并打印所有日历列表。 但是当我想要创建一个新事件时,响应是403 Forbidden。

我的代码:

require_once 'src/Google_Client.php';
require_once 'src/contrib/Google_CalendarService.php';

session_start();

const CLIENT_ID = 'XXXXXXXXXX.apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME = 'XXXXXXXXXXXX@developer.gserviceaccount.com';

const KEY_FILE = 'google_src/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-privatekey.p12';

$client = new Google_Client();
$client->setApplicationName("APP_NAME");

if (isset($_SESSION['token'])) {
 $client->setAccessToken($_SESSION['token']);
}

$key = file_get_contents(KEY_FILE);
$client->setClientId(CLIENT_ID);
$client->setAssertionCredentials(new Google_AssertionCredentials(
  SERVICE_ACCOUNT_NAME,
  'https://www.google.com/calendar/feeds/MY_CALENDAR_ID/private/full/',
  $key)
);

$client->setClientId(CLIENT_ID);
$cal = new Google_CalendarService($client);
$event = new Google_Event();
$event->setSummary('Bon dia pel matí!');
$event->setLocation('Somewhere');
$start = new Google_EventDateTime();
$start->setDateTime('2012-08-06T10:00:00.000-07:00');
$event->setStart($start);
$end = new Google_EventDateTime();
$end->setDateTime('2012-09-06T10:25:00.000-07:00');
$event->setEnd($end);

$createdEvent = $cal->events->insert(CALENDAR_NAME, $event);

服务器响应:

(403) Forbidden

有什么想法吗?也许其他范围,或允许编辑事件(我不知道我可以配置这个,我已经搜索过,承诺)

当然,我的日历是公开日历。

欢迎任何帮助:)

谢谢!

1 个答案:

答案 0 :(得分:13)

我解决了,问题不是代码,它工作正常。

问题在于分享我的日历。我不知道的东西。

要允许您在服务帐户中修改日历活动,但在Google Api控制台中执行的所有步骤都不够,您必须通过日历配置(日历配置 - >分享此日历)向电子邮件授予权限帐户指定到Google Api控制台(电子邮件地址)。

如果是商业帐户,则可能会出现问题,然后您必须与帐户管理员联系。

此处显示的代码工作正常,所以我希望它可以帮助某人,只需记住您必须使用475版本的Google Api客户端。

您可以在以下链接找到:http://google-api-php-client.googlecode.com/svn/trunk/src/