2016-10-03 112 views
1

我一直在使用谷歌日曆API多年來取得了巨大成功。我使用curl使用來自php的請求。谷歌聯繫人API PHP捲曲檢索聯繫人

我一直在努力實現與聯繫人api相同。目前我所要做的就是將聯繫人列表作爲json獲取,我已經通過OAuth2獲取了一個令牌。我正在使用下面的簡單代碼進行測試。

$accessToken = "thebiglongaccesstoken1234"; 
$userMail = "[email protected]"; 
$requestURL ="https://www.google.com/m8/feeds/contacts/".$userMail."/full?v=3.0&alt=json"; 
$headers = array("Authorization: OAuth ".$accessToken); 

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_URL, $requestURL); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

$response = curl_exec($ch); 
$responseArray = json_decode($response, TRUE); 

var_dump($responseArray); 
print '<br/>Response: <pre>' . print_r($responseArray, true) . '</pre>'; 
print 'curl Error: <pre>' . print_r(curl_error($ch), true) . '</pre>'; 

在瀏覽器的響應是:

NULL 響應: 捲曲錯誤:

我能得到這個請求,在谷歌的OAuth 2.0遊樂場工作。

我不想使用庫,因爲我最終想要做的就是通過電子郵件地址找到用戶聯繫人以獲取電話號碼。我已經爲日曆創建了自己的函數庫,因此不想添加額外的未使用的東西。它應該只需要幾行代碼。

我想我現在已經盯着它太久了,而且很想念那顯而易見的東西。任何幫助讚賞。

UPDATE:

我使用curl_getinfo()得到這個出

陣列 ( [URL] =>https://www.google.com/m8/feeds/contacts/[email protected]/full?v=3.0&alt=json [內容] => text/html的;字符集= UTF-8 [HTTP_CODE ] => 401 [header_size] => 457 [request_size] => 208 [FILETIME] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [TOTAL_TIME] => 0.1244 [namelookup_time] => 0.000837 [CONNECT_TIME] => 0.00306 [pretransfer_time] => 0.015428 [size_upload] => 0 [size_download] => 11875 [speed_download] => 95458 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => 0 [starttransfer_time] => 0.123992 [redirect_time] => 0 [certinfo] =>數組 ( )

+0

你可以在將輸出傳遞給'json_decode'之前打印輸出嗎? Contacts API通常會提供純文本錯誤。此外,響應狀態代碼也會有幫助。 –

+0

如果我嘗試使用$ response來做任何事情,例如打印它或var_dump,我會收到帶有401或404錯誤的破損機器人谷歌屏幕,並顯示消息「請求中存在錯誤,這就是我們所知道的。 curl錯誤上的var_dump顯示空字符串。 –

回答

2

看到這是一個401,我注意到你的授權標題不完全正確。將您的授權標題更改爲"Authorization: Bearer " . $accessToken而不是"Authorization: OAuth " . $accessToken

如果你仍然得到一個401,那麼你需要得到一個新的訪問令牌。

+0

感謝您的回答。試過還是一樣的。這些令人沮喪的問題之一。 –