Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Guzzle with basic auth and bearer token

Tags:

php

guzzle6

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

like image 359
Oskar Calvo Avatar asked Jun 25 '16 14:06

Oskar Calvo


People also ask

What is guzzle in laravel?

A Guzzle is a PHP HTTP CLIENT that we use to send HTTP requests for trivial integration with web services , such as: PATCH. PUT. GET. DELETE.

What is bearer access token?

Bearer Tokens are the predominant type of access token used with OAuth 2.0. A Bearer Token is an opaque string, not intended to have any meaning to clients using it. Some servers will issue tokens that are a short string of hexadecimal characters, while others may use structured tokens such as JSON Web Tokens.

How do you set headers in guzzle?

Each key is the name of a header, and each value is a string or array of strings representing the header field values. // Set various headers on a request $client->request('GET', '/get', [ 'headers' => [ 'User-Agent' => 'testing/1.0', 'Accept' => 'application/json', 'X-Foo' => ['Bar', 'Baz'] ] ]);


2 Answers

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();
like image 171
revo Avatar answered Oct 18 '22 20:10

revo


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();
like image 5
Nicky Kouffeld Avatar answered Oct 18 '22 18:10

Nicky Kouffeld