我在Guzzle 4.2上获得了401,同样的设置也适用于Postman。代码如下。
// Create a client with a base URL
$client = new GuzzleHttp\Client(['base_url' => 'cloud.feedly.com/v3/streams/contents?streamId=user/user-id/category/global.all&count=1']);
// Send a request to https://github.com/notifications
$response = $client->get();
//Auth
$response->addHeader('Authorization', "auth-code");
//send
$r = $response->send();
dd($r->json());
错误是:
GuzzleHttp \ Exception \ ClientException (401)
Client error response [url] cloud.feedly.com/v3/streams/contents?streamId=user/user-id/global.all&count=1 [status code] 401 [reason phrase] Unauthorized
答案 0 :(得分:4)
根据此行查看documentation page:
$response = $client->get();
它将发送一个获取请求,没有授权,因此401响应。
请尝试以下
// Create a client with a base URL.
$client = new GuzzleHttp\Client();
$request = $client-> createRequest('GET', 'cloud.feedly.com/v3/streams/contents?streamId=user/user-id/category/global.all&count=1');
$request->setHeader('Authorization', "auth-code");
// Send.
$response = $client->send($request);
dd($response->json());
以上创建请求,在其上设置授权标头。然后一旦准备就实际发送它。
我认为它在Postman中有效,因为你的标题已设置,如果删除了授权标题,它也可能会在那里失败。
我没有对此进行测试,但认为它会起作用。
答案 1 :(得分:0)
$response = $client->get('GET', 'cloud.feedly.com/v3/streams/contents?streamId=user/user-id/category/global.all&count=1', ['auth' => ['YOUR_USERNAME', 'YOUR_PASSWORD']]);
401错误意味着您应该使用有效的用户名和密码验证自己
此致