2013-08-23 40 views
5

好了,這裏就是我想要做的......取PHP服務器上的谷歌日曆事件在iOS應用驗證後

  1. 用戶授權與讀取iPhone上的應用程序/寫訪問谷歌日曆
  2. 我得到一些從谷歌
  3. 傳遞一個身份驗證令牌的令牌到我的PHP服務器,並將其保存在數據庫
  4. 使用令牌上的谷歌事件,定期檢查,並將它們存儲在服務器的數據庫
  5. 發送goog將事件數據作爲json發送到應用程序

我想實現在服務器上獲取Google事件,以便可以在其周圍構建其他功能,例如發送遠程推送通知。

我一直在獲取auth令牌並將其保存在服務器上,並使用它從Google的日曆api中獲取事件,但無法找到相同的資源。有人可以對此有所瞭解。


UPDATE

我已經能夠成功地實施該方案,直到第3步

我拿到身份驗證令牌和刷新令牌從谷歌和它保存在數據庫中。現在使用Google API php client,我試圖使用前面提到的訪問令牌連接到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 Fatal error: Uncaught exception 'Google_AuthException' with message 'Could not json decode the token' in /myserver/application/libraries/Google/auth/Google_OAuth2.php:162

Stack trace: /myserver/application/libraries/Google/Google_Client.php(170): Google_OAuth2->setAccessToken('a_token...')

我明白,我直接想設置在谷歌客戶端API的訪問令牌不指定當用戶授權訪問服務器上的Google日曆時通常使用的任何重定向網址或其他參數。這應該像我想要的那樣工作嗎?


更新2

在一些進一步的挖掘,我發現使用setAccessToken作爲谷歌的客戶端不能正常工作的API直接設置訪問令牌預計,在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的。

Error refreshing the OAuth2 token, message: '{"error" : "invalid_request"}'

回答

3

最後我能回答我自己的問題。希望這可以幫助別人。

步驟1 & 2.我得到了谷歌OAuth協議我的iOS應用程序的工作按照this excellent tutorial on mobiletuts的說明。

第3步。我在我的Web服務器上的數據庫中保存了訪問令牌和刷新令牌。

步驟4.使用刷新標記我從iOS應用得到了我連接到與此代碼的谷歌日曆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); 
+0

您可以調用'Google_Client :: refreshToken後取出兩行'。該函數的代碼中的註釋是「使用給定的刷新令牌獲取新的OAuth 2.0訪問令牌」,並且確實將該新訪問令牌設置爲客戶端的當前訪問令牌。 – Jonathan

+0

...此外,除非您設置客戶端密碼以及客戶端ID,否則這不起作用,或者我錯過了某些東西? (N.B.'client_id'和'client_secret'都可以在Google API PHP Client的/ a config文件中設置。) – Jonathan

+0

@dvijaz - 感謝關於訪問令牌的提示。就'client_secret'而言,代碼在不設置'client_secret'的情況下工作。 – vikmalhotra

相關問題