目前正在构建一个Laravel应用程序,该应用程序使用Socialite包成功验证使用谷歌凭据的用户。然而,我正试图向谷歌服务器发出GET请求,以检索给定用户的联系人列表,我在谷歌oAuth 2 Playground上进行了一些实验,并试图在我的应用程序中模拟相同的请求。我创建了以下功能:
public function getContactList()
{
$client = new GuzzleHttpClient();
$email = Auth::user()->email;
$token = Session::get('token');
$json = $client->get('https://www.google.com/m8/feeds/contacts/default/full/', [
'headers' => [
'Authorization' => 'Bearer ' . $token,
],
]);
dd($json);
return $json;
}
经过无休止的努力,我终于得到了一个肯定的答案,但它没有用,身体里什么都没有,用Json_decode解码会得到null,下面是答案:
Response {#198 ▼
-reasonPhrase: "OK"
-statusCode: 200
-effectiveUrl: "https://www.google.com/m8/feeds/contacts/default/full/"
-headers: array:11 [▼
"expires" => array:1 [▼
0 => "Mon, 30 Mar 2015 15:19:52 GMT"
]
"date" => array:1 [▼
0 => "Mon, 30 Mar 2015 15:19:52 GMT"
]
"cache-control" => array:1 [▶]
"vary" => array:2 [▶]
"content-type" => array:1 [▶]
"x-content-type-options" => array:1 [▶]
"x-frame-options" => array:1 [▶]
"x-xss-protection" => array:1 [▶]
"content-length" => array:1 [▶]
"server" => array:1 [▶]
"alternate-protocol" => array:1 [▶]
]
-headerNames: array:11 [▼
"expires" => "Expires"
"date" => "Date"
"cache-control" => "Cache-Control"
"vary" => "Vary"
"content-type" => "Content-Type"
"x-content-type-options" => "X-Content-Type-Options"
"x-frame-options" => "X-Frame-Options"
"x-xss-protection" => "X-XSS-Protection"
"content-length" => "Content-Length"
"server" => "Server"
"alternate-protocol" => "Alternate-Protocol"
]
-body: Stream {#197 ▼
-stream: :stream {@8 ▼
wrapper_type: "PHP"
stream_type: "TEMP"
mode: "w+b"
unread_bytes: 0
seekable: true
uri: "php://temp"
options: []
}
-size: null
-seekable: true
-readable: true
-writable: true
-uri: "php://temp"
-customMetadata: []
}
-protocolVersion: "1.1"
}
我可以更改什么或需要更改什么才能获得完整的联系人列表,而不是空的200回复?
更新:我做了一些测试来验证我的请求的准确性,发现上面的请求实际上返回了一个ATOM提要,这可能是问题所在。当我向返回JSON响应的Drive API发出请求时,我可以通过使用JSON_decode解析适当的数据,而不会遇到任何麻烦。我需要使用哪个函数来解析PHP中的ATOM数据才能检索它?
您是否尝试将"alt=json"参数添加到GET请求中?像这样:
$response = $client->get('https://www.google.com/m8/feeds/contacts/default/full?alt=json', [
'headers' => [
'Authorization' => 'Bearer ' . $token,
],
]);
我一直在尝试获取JSON中的联系人API,看起来这是正确的方法:https://developers.google.com/google-apps/contacts/v3/reference#contacts-查询参数引用
您将从Guzzle获得一个Response
对象。Response
对象上有一个可用的json
方法,因此您应该能够:
$response = $client->get('https://www.google.com/m8/feeds/contacts/default/full/', [
'headers' => [
'Authorization' => 'Bearer ' . $token,
],
]);
echo $response->json();
来源:http://guzzle.readthedocs.org/en/latest/http-messages.html#id2