PHP Guzzle with basic auth and bearer token

谁说胖子不能爱 提交于 2019-12-10 13:05:16

问题


I'm trying to make a connection with infojobs-api, the documentation explian how to make it in this way:

GET /api/1/application HTTP/1.1
Host: api.infojobs.net Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==,Bearer 07d18fac-77ea-461f-9bfe-a5e9d98deb3d

(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:

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

So I'm doing something wrong, but I don't find what it's wrong.

Any idea, thanks.

Oskar


回答1:


As I'm seeing your request's HTTP headers:

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.

What you should do is setting Authorization header manually:

$newresponse = $basicauth->request(
    'GET',
    'api/1/curriculum',
    ['debug'   => true], 
    ['headers' => 
        [
            'Authorization' => "Basic {$credentials},Bearer {$acceso->access_token}"
        ]
    ]
)->getBody()->getContents();


来源:https://stackoverflow.com/questions/38029422/php-guzzle-with-basic-auth-and-bearer-token

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