加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 17:11:04 所属栏目:PHP教程 来源:网络整理
导读:正在开发导出并使用Codeigniter和Google apiService / apiCalendarService导入Go??ogle Calendar应用程序.当我从此页面向Google授权我的应用程序时,我已经完成了所有设置,没有任何问题: 包含的代码是重定向发生时发生的情况: session_start();require(APPP
正在开发导出并使用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']);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读