简体   繁体   English

带有基本身份验证和不记名令牌的 PHP Guzzle

[英]PHP Guzzle with basic auth and bearer token

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获取 /api/1/应用程序 HTTP/1.1
Host: api.infojobs.net Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==,Bearer 07d18fac-77ea-461f-9bfe-a5e9d98deb3d主机:api.infojobs.net 授权:基本 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 a 401 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": /app/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:107 中的“客户端凭据无效”、“时间戳”:“2016-06-25T14:08:54.774Z”}

So I'm doing something wrong, but I don't find what it's wrong.所以我做错了什么,但我没有找到问题所在。

Any idea, thanks.任何想法,谢谢。

Oskar奥斯卡

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. 您有一个Authorization标头,其中包含逗号分隔值。 They are not apart from each other. 他们并没有彼此分开。 So you can't benefit from Guzzle's auth key like what you have done. 所以你不能像你所做的那样从Guzzle的auth密钥中受益。

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

For a post call this works for me:对于一个帖子电话,这对我有用:

$guzzle = new Client(['base_uri' => self::APIURL]);

$raw_response = $guzzle->post($endpoint, [
  'headers' => [ 'Authorization' => 'Bearer ' . $publicKey ],
  'body' => json_encode($data),
]);

$response = $raw_response->getBody()->getContents();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM