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

vik*_*tra 5 php authentication google-calendar-api ios

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

  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);
Run Code Online (Sandbox Code Playgroud)

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

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);
Run Code Online (Sandbox Code Playgroud)

现在我得到的错误是一个invalid_request.

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

vik*_*tra 3

我终于能够回答我自己的问题了。希望这对其他人有帮助。

第 1 步和第 2 步。按照有关 mobiletuts 的优秀教程中的说明,我让 Google OAuth 协议在我的 iOS 应用程序中正常运行。

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

步骤 4. 使用从 iOS 应用程序获得的刷新令牌,我使用此代码连接到 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);
Run Code Online (Sandbox Code Playgroud)