繁体   English   中英

在iOS应用上进行身份验证后,在PHP服务器上获取Google Calendar事件

[英]Fetch Google Calendar Events on PHP server after authenticating on iOS app

好的,这就是我想要做的......

  1. 用户授权iPhone上的应用程序具有对Google日历的读/写权限
  2. 我从Google获得了某种身份验证令牌
  3. 将令牌传递给我的PHP服务器并将其保存在数据库中
  4. 使用令牌定期检查Google事件并将其存储在服务器的数据库中
  5. 将Google事件数据作为json发送到应用

我想在服务器上实现Google事件的获取,以便我可以围绕它们构建其他功能,例如发送远程推送通知。

我被困在获取身份验证令牌并将其保存在服务器上并使用它从Google的日历api中获取事件,但无法找到相同的资源。 有人可以对此有所了解。


UPDATE

我已经能够成功实施该方案直到第3步。

我从Google获取身份验证令牌和刷新令牌并将其保存在数据库中。 现在使用Google API php客户端 ,我正在尝试使用我之前获得的访问令牌连接到Google的日历API。 我使用以下代码...

require_once(APPPATH . "libraries/Google/Google_Client.php");
require_once(APPPATH . "libraries/Google/contrib/Google_CalendarService.php");

$client = new Google_Client();
$client->setAccessToken($google_access_token);

$calendar = new Google_CalendarService($client);
$calendarList = $calendar->calendarList->listCalendarList();
echo print_r($calendarList, true);

但现在我收到了这个错误......

PHP致命错误:未捕获的异常'Google_AuthException',并在/myserver/application/libraries/Google/auth/Google_OAuth2.php:162中显示消息'无法json解码令牌'

堆栈跟踪:/myserver/application/libraries/Google/Google_Client.php(170):Google_OAuth2-> setAccessToken('a_token ...')

我了解到,我直接尝试在Google客户端api中设置访问令牌,而不指定任何重定向网址或用户授权访问服务器本身上的Google日历时通常使用的其他参数。 这应该像我想的那样工作吗?


更新2

进一步挖掘后,我发现使用setAccessToken直接设置访问令牌不起作用,因为API的Google客户端需要在setAccessToken方法中使用JSON编码的字符串。 经过一些调整后,我将代码更改为以下....

require_once(APPPATH . "libraries/Google/Google_Client.php");
require_once(APPPATH . "libraries/Google/contrib/Google_CalendarService.php");

$client = new Google_Client();
$client->setAccessType('offline');
$client->refreshToken($google_refresh_token);
$newToken = $client->getAccessToken();
$client->setAccessToken($newToken);

$calendar = new Google_CalendarService($client);
$calendarList = $calendar->calendarList->listCalendarList();
echo print_r($calendarList, true);

现在我得到的错误是invalid_request的错误。

刷新OAuth2令牌时出错,消息:'{“error”:“invalid_request”}'

最后,我能够回答我自己的问题。 希望这有助于其他人。

步骤1和2.我按照这个关于mobiletuts的优秀教程中的说明,在我的iOS应用程序中使用了Google OAuth协议。

步骤3.我在Web服务器上的数据库中保存了访问令牌和刷新令牌。

步骤4.使用我从iOS App I获得的刷新令牌,使用此代码连接到Google日历API ...

require_once(APPPATH . "libraries/Google/Google_Client.php");
require_once(APPPATH . "libraries/Google/contrib/Google_CalendarService.php");

/* setup google client object */
$client = new Google_Client();
$client->setClientId(GOOGLE_CLIENT_ID);

/* refresh access token */
$client->refreshToken($google_refresh_token);
$newToken = $client->getAccessToken();
$client->setAccessToken($newToken);

/* get google calendars list */
$calendar = new Google_CalendarService($client);
$calendarList = $calendar->calendarList->listCalendarList();
echo print_r($calendarList, true);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM