带有基本身份验证和不记名令牌的 PHP Guzzle
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38029422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Guzzle with basic auth and bearer token
提问by Oskar Calvo
I'm trying to make a connection with infojobs-api, the documentation explian how to make it in this way:
我正在尝试与 infojobs-api 建立连接,文档解释了如何以这种方式进行连接:
GET /api/1/application HTTP/1.1
Host: api.infojobs.net Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==,Bearer 07d18fac-77ea-461f-9bfe-a5e9d98deb3d
GET /api/1/application HTTP/1.1
Host: api.infojobs.net 授权: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==,Bearer 07d18fac-77ea-461f-9bfe-a5e9d98deb3d
(https://developer.infojobs.net/documentation/user-oauth2/index.xhtml)
( https://developer.infojobs.net/documentation/user-oauth2/index.xhtml)
And this is my code:
这是我的代码:
$basicauth = new Client(['base_uri' => 'https://api.infojobs.net']);
$credentials = base64_encode(CLIENT_ID .':' . CLIENT_SECRET ) ;
$newresponse = $basicauth->request(
'GET',
'api/1/curriculum',
['debug' => true],
['auth' =>
['Basic', $credentials] ,
['Bearer', $acceso->access_token]
]
)->getBody()->getContents();
d($newresponse);
The API/Guzlle give me back this error:
API/Guzlle 给了我这个错误:
Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error:
GET https://api.infojobs.net/api/1/curriculum
resulted in a401 No Autorizado
response: {"error":"102","error_description":"Client credentials not valid","timestamp":"2016-06-25T14:08:54.774Z"} in /app/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:107
致命错误:未捕获的 GuzzleHttp\Exception\ClientException:客户端错误:
GET https://api.infojobs.net/api/1/curriculum
导致401 No Autorizado
响应:{"error":"102","error_description":"客户端凭据无效","timestamp":"2016-06-25T14:08 :54.774Z"} 在 /app/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:107
So I'm doing something wrong, but I don't find what it's wrong.
所以我做错了什么,但我没有发现它有什么问题。
Any idea, thanks.
任何想法,谢谢。
Oskar
奥斯卡
回答by revo
As I'm seeing your request's HTTP headers:
当我看到您的请求的 HTTP 标头时:
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==,Bearer 07d18fac-77ea-461f-9bfe-a5e9d98deb3d
You have an Authorization
header which contains a comma separated value. They are not apart from each other. So you can't benefit from Guzzle's auth
key like what you have done.
您有一个Authorization
包含逗号分隔值的标题。他们彼此不分开。所以你不能auth
像你所做的那样从 Guzzle 的密钥中受益。
What you should do is setting Authorization header manually:
您应该做的是手动设置 Authorization 标头:
$newresponse = $basicauth->request(
'GET',
'api/1/curriculum',
['debug' => true],
['headers' =>
[
'Authorization' => "Basic {$credentials},Bearer {$acceso->access_token}"
]
]
)->getBody()->getContents();