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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了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日历帐户,直到我抛出此@L_360_11@:

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 – Google Calendar API出错 – 401添加日历活动时需要登录全部内容,希望文章能够帮你解决php – Google Calendar API出错 – 401添加日历活动时需要登录所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。