Google日历API错误 - 401添加日历活动时需要登录 [英] Error with Google Calendar API - 401 Login required when adding a calendar event
问题描述
由于我正在开发导出并使用Codeigniter和Google apiService / apiCalendarService导入Google日历应用程序。我通过此页面向Google授权我的应用程序时,完全设置了所有设置,没有问题:
As I am developing an exportation and importing into Google Calendar application with Codeigniter and the Google apiService/apiCalendarService. I have everything set up perfectly, no problem with this, when I authorize my application with Google from this page:
包含的代码是在发生重定向时发生的情况:
The contained code is what happens when the redirect happens:
session_start();
require(APPPATH . 'libraries/google/apiClient.php');
require(APPPATH . 'libraries/google/contrib/apiCalendarService.php');
$apiClient = new apiClient();
$apiClient->setUseObjects(true);
$calendarService = new apiCalendarService($apiClient);
if(isset($_SESSION['oauth_access_token']))
{
$apiClient->setAccessToken($_SESSION['oauth_access_token']);
}
else
{
$token = $apiClient->authenticate();
$_SESSION['oauth_access_token'] = $token;
}
验证后,我被重定向回我的localhost网站,网址:
After authenticating, I get redirected back to my localhost site with the options in my URL:
http://localhost/project/acp/sync/auth/?code=4/DS91JtSJ5_9Q-Z55Kpyh2AicyWdL
我的脚本将通过使用 auth检查URI段来检测是否已进行认证
和代码
查询字符串 - 因此我可以开始将活动导入我的Google日历帐户,直到我收到此错误:
My script will detect when an authencation has been made by checking the URI segment with auth
and a code
query string - so I can start importing events to my Google Calendar account, until that I get this error thrown:
Fatal error: Uncaught exception 'apiServiceException' with message 'Error calling POST https://www.googleapis.com/calendar/v3/calendars/primary/events?key={removed API key}: (401) Login Required' in /Users/Me/Sites/project/application/libraries/google/io/apiREST.php:86
Stack trace:
#0 /Users/Me/Sites/project/application/libraries/google/io/apiREST.php(56): apiREST::decodeHttpResponse(Object(apiHttpRequest))
#1 /Users/Me/Sites/project/application/libraries/google/service/apiServiceResource.php(187): apiREST::execute(Object(apiServiceRequest))
#2 /Users/Me/Sites/project/application/libraries/google/contrib/apiCalendarService.php(493): apiServiceResource->__call('insert', Array)
#3 /Users/Me/Sites/project/application/controllers/acp/panel.php(2053): EventsServiceResource->insert('primary', Object(Event))
#4 [internal function]: Panel->sync('auth')
#5 /Users/Me/Sites/project/system/core/CodeIgniter.php(359): call_user_func_array(Array, Array)
#6 /Users/Me in /Users/Me/Sites/project/application/libraries/google/io/apiREST.php on line 86
导入事件的代码(代码来自Google API示例):
The code that was importing an event (code came from Google API samples):
session_start();
require(APPPATH . 'libraries/google/apiClient.php');
require(APPPATH . 'libraries/google/contrib/apiCalendarService.php');
$apiClient = new apiClient();
$apiClient->setUseObjects(true);
$service = new apiCalendarService($apiClient);
$event = new Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new EventDateTime();
$start->setDateTime('2011-06-03T10:00:00.000-07:00');
$event->setStart($start);
$end = new EventDateTime();
$end->setDateTime('2011-06-03T10:25:00.000-07:00');
$event->setEnd($end);
$attendee1 = new EventAttendee();
$attendee1->setEmail('attendeeEmail');
// ...
$attendees = array($attendee1,
// ...
);
$event->attendees = $attendees;
$createdEvent = $service->events->insert('primary', $event);
echo $createdEvent->getId();
我尝试Googling的错误,但我似乎没有找到任何解决方案,
I tried Googling the error, but I don't seem to find any solutions with this, do you know how I can fix this problem?
推荐答案
您需要一个 401登录
因为第二个代码没有设置标记:
You are getting a 401 Login Required
because the second code is not setting the token:
$apiClient->setAccessToken($_SESSION['oauth_access_token']);
这篇关于Google日历API错误 - 401添加日历活动时需要登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!