我已经激活了 google API 控制台https://code.google.com/apis/console并且我创建了一个项目,包括 OAuth 凭据。我选择了“桌面应用程序”设置,因为这个应用程序将在浏览器之外运行,从 CLI(实际上只在我的计算机上)。
我还下载了此处描述的客户端库:https ://code.google.com/p/google-api-php-client/ 。
我已经编辑了编辑google-api-php-client/src/config.php
键,,,,application_name
而oauth2_client_id
没有别的。oauth2_client_secret
oauth2_redirect_uri
developer_key
我也删除了所有其他服务services
。
现在,我有以下应用程序,这些应用程序来自指南中的示例:
<?php
require_once 'google-api-php-client/src/apiClient.php';
require_once "google-api-php-client/src/contrib/apiCalendarService.php";
session_start();
$apiClient = new apiClient();
$apiClient->setUseObjects(true);
$service = new apiCalendarService($apiClient);
if (isset($_SESSION['oauth_access_token'])) {
$apiClient->setAccessToken($_SESSION['oauth_access_token']);
} else {
$token = $apiClient->authenticate();
$_SESSION['oauth_access_token'] = $token;
}
$event = new Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new EventDateTime();
$start->setDateTime('2011-06-03T10:00:00.000-07:00');
$start->setTimeZone('America/Los_Angeles');
$event->setStart($start);
$end = new EventDateTime();
$end->setDateTime('2011-06-03T10:25:00.000-07:00');
$end->setTimeZone('America/Los_Angeles');
$event->setEnd($end);
$event->setRecurrence(array('RRULE:FREQ=WEEKLY;UNTIL=20110701T100000-07:00'));
$attendee1 = new EventAttendee();
$attendee1->setEmail('attendeeEmail');
// ...
$attendees = array($attendee1,
// ...
);
$event->attendees = $attendees;
$recurringEvent = $service->events->insert('primary', $event);
echo $recurringEvent->getId();
但是我收到以下错误:
Fatal error: Uncaught exception 'apiServiceException' with message 'Error calling POST https://www.googleapis.com/calendar/v3/calendars/primary/events?key=<the-key>: (401) Login Required' in /home/flav/projects/.../google-api-php-client/src/io/apiREST.php on line 86
apiServiceException: Error calling POST https://www.googleapis.com/calendar/v3/calendars/primary/events?key=<the-key>: (401) Login Required in /home/flav/projects/.../google-api-php-client/src/io/apiREST.php on line 86
Call Stack:
0.0003 252600 1. {main}() /home/flav/projects/.../gcal-api.php:0
0.0202 1769344 2. EventsServiceResource->insert() /home/flav/projects/.../gcal-api.php:38
0.0202 1770288 3. apiServiceResource->__call() /home/flav/projects/.../google-api-php-client/src/contrib/apiCalendarService.php:493
0.0206 1781864 4. apiREST::execute() /home/flav/projects/.../google-api-php-client/src/service/apiServiceResource.php:187
0.2342 1794848 5. apiREST::decodeHttpResponse() /home/flav/projects/.../google-api-php-client/src/io/apiREST.php:56
如何解决这个问题?
哦,在 google API 控制台上,我有两个值和Redirect URIs:
,我应该使用哪一个?urn:ietf:wg:oauth:2.0:oob
http://localhost
oauth2_redirect_uri