Why is my Authorization Header giving me a 401 in Guzzle?

大兔子大兔子 提交于 2019-12-01 22:50:22
Shaun

Looking at the documentation page as per this line:

$response = $client->get();

It will send a get request, with no authorisation, hence the 401 response.

Try the following instead

// 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());

The above creates a request, set the authorisation header on it. Then once prepared it actually sends it.

I think it worked in Postman because your headers are set, if you remove the authorization header it will likely fail there too.

I have not tested this but think it will work.

$response = $client->get('GET', 'cloud.feedly.com/v3/streams/contents?streamId=user/user-id/category/global.all&count=1', ['auth' =>  ['YOUR_USERNAME', 'YOUR_PASSWORD']]);

The 401 error means you should authenticate yourself with a valid username and password

Regards

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!