php – Google Calendar API出错 – 401添加日历活动时需要登录

正在开发导出并使用Codeigniter和Google ApiService / apiCalendarService导入Go​​ogle Calendar应用程序.当我从此页面向Google授权我的应用程序时,我已经完成了所有设置,没有任何问题:

包含的代码重定向发生时发生的情况:

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;
}

在进行身份验证后,我会使用我的URL中的选项重定向回我的localhost站点

http://localhost/project/acp/sync/auth/?code=4/DS91JtSJ5_9Q-Z55Kpyh2AicyWdL

我的脚本将通过使用auth和代码查询字符串检查URI段来检测何时进行了身份验证 – 因此我可以开始将事件导入我的Google日历帐户,直到我抛出此错误

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示例):

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();

我试过谷歌搜索错误,但我似乎没有找到任何解决方案,你知道我怎么能解决这个问题?

解决方法:

您将获得401登录要求,因为第二个代码未设置令牌:

$apiclient->setAccesstoken($_SESSION['oauth_access_token']);

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...