谷歌日历API服务帐户-错误403


Google Calendar API Service Accounts - Error 403

我有一个问题我不能解决。我正在尝试在公共日历中插入一个事件。我设法做到了,但我不希望用户登录,所以我尝试了服务帐户方法,但我不能让它工作。

问题:

Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling POST https://www.googleapis.com/calendar/v3/calendars/rentyourparis.com_h4s4cm7b8e27of4oigpl4651co%40group.calendar.google.com/events?key=AIzaSyA5xDFe5UjuleS-pssxoXTLOsT0ZA6dur0: (403) Access Not Configured. Please use Google Developers Console to activate the API for your project.' in /home/rentyour/www/google-api-php-client/src/Google/Http/REST.php:79 Stack trace: #0 /home/rentyour/www/google-api-php-client/src/Google/Http/REST.php(44): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request)) #1 /home/rentyour/www/google-api-php-client/src/Google/Client.php(505): Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request)) #2 /home/rentyour/www/google-api-php-client/src/Google/Service/Resource.php(195): Google_Client->execute(Object(Google_Http_Request)) #3 /home/rentyour/www/google-api-php-client/src/Google/Service/Calendar.php(1459): Google_Service_Resource->call('insert', Array, 'Google_Service_...') #4 /h in /home/rentyour/www/google-api-php-client/src/Google/Http/REST.php on line 79

•日历API是ON

•我使用了Service Accounts凭据

•删除所有引用

代码如下:

<?php
session_start();

$path = 'google-api-php-client/src/';  
$oldPath = set_include_path(get_include_path() . PATH_SEPARATOR . $path);
require_once 'Google/Client.php';
require_once 'Google/Service/Calendar.php';

 define('SERVICE_ACCOUNT_NAME', 'XXXXXX-50dv2bprcmuhn6tuicavg2oqc3c692ua@developer.gserviceaccount.com');
/* THE PATH TO THE SECRET KEY GENERATED WHEN YOU REQUESTED THE
 * SERVICE ACCOUNT. The key's name contains it's public key
 * fingerprint, represented below by the string <longhexstring>
 */
define('KEY_FILE', 'XXXXXXXXXXXXXX.p12');
$client = new Google_Client();
$client->setApplicationName("My TEST Application");
/* Note: make sure to call $client->setUseObjects(true) if you want to see
* objects returned instead of data (this example code uses objects)
 */
/* If you already have a session token, use it. Normally this token is
 * stored in a database and you'd need to retrieve it from there. For
 * this demo we'll simply start a new session each time.
 */
session_start();
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
/* Load the key in PKCS 12 format - remember: this is the file you had to
* download when you created the Service account on the API console.
*/
$key = file_get_contents(KEY_FILE);
$client->setAssertionCredentials(new Google_Auth_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/calendar'),
$key)
);
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
/* ------------------------- We are now properly authenticated ------------------- */
$cal = new Google_Service_Calendar($client);
$event = new Google_Service_Calendar_Event();
$event->setSummary('Dinner at Henks house');
               /* what to do, summary of the appointment */
$event->setLocation('Slochteren');            /* yes, it exists */
/* Now, set the start date/time
*/
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2014-08-10T19:00:00.000+01:00'); /* Or e.g. 2010-08-26T10:40:00+02:00 */
$event->setStart($start);
/* Now, set the end date/time
*/
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2014-08-10T22:00:00.000+01:00'); /*  2010-08-26T10:40:00+02:00 */
$event->setEnd($end);
/* For now I just set one attendee, but you can create lists of many if you want to
*/
$attendee1 = new Google_Service_Calendar_EventAttendee();
$attendee1->setEmail('test@gmail.com');
$attendees = array($attendee1);
$event->attendees = $attendees;
/* CREATE THE EVENT IN THE PROPER CALENDAR
*/
$createdEvent = $cal->events-    >insert('XXXXXXXXXXXgpl4651co@group.calendar.google.com', $event);
print "<html><body>";
echo "<pre>I created a calendar entry, it's id is '" . $createdEvent->id . "'</pre>";
print "</body></html>";
 /* Here should be some code to store the token in the database again.
 * Just to reminds us we put some useless code here ;-P
 */
if ($client->getAccessToken()) {
  $_SESSION['token'] = $client->getAccessToken();
 }
?>

我已经搜索和测试了几个小时,但没有任何工作正常:(

谢谢你的帮助

尽管日历是公开的,但这并不意味着任何人都可以在其中写入内容。为此,您需要:

  • 与您的服务帐户共享此公共日历。
  • 在服务帐号的日历上创建事件,并邀请公共日历。
  • 或者如果日历是域拥有的,您可以将您的服务帐户设置为域的朋友(https://developers.google.com/drive/web/delegation)。

我在Google Apps的管理控制台授权了服务帐户client_id,并从API控制台删除了Referees选项卡中的ip。这对我很有效。希望对大家有所帮助