ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-06-26 07:25:39  阅读:207  来源: 互联网

标签:php authentication debugging codeigniter google-calendar-api


正在开发导出并使用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']);

标签:php,authentication,debugging,codeigniter,google-calendar-api
来源: https://codeday.me/bug/20190626/1291695.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有