目前正在构建一个Laravel应用程序,该应用程序使用Socialite程序包成功使用其Google凭据对用户进行身份验证。但是,我尝试向Google服务器发送GET请求以检索给定用户的联系人列表,我一直在Google oAuth 2 Playground上尝试一下,并尝试在我的应用内模拟相同的请求。我创建了以下函数:
public function getContactList()
{
$client = new \GuzzleHttp\Client();
$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提要,这可能是问题所在。当我向Drive API发出返回JSON响应的请求时,我只需使用json_decode解析它就可以毫无困难地提取相应的数据。我需要使用哪个函数来解析PHP中的ATOM数据才能检索它?
答案 0 :(得分:1)
您是否尝试添加' 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-query-parameters-reference
答案 1 :(得分:0)
你从Guzzle得到一个Response
对象。 json
对象上有Response
方法,因此您应该能够:
$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