Jesse Parker
Jesse Parker

Reputation: 156

php-curl not sending Authorization header after redirect

I'm using PHP 7.2 and cURL 7.53.1. I'm trying to use Google's NEST REST API.

I try a CURL request like this:

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://developer-api.nest.com",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_FOLLOWLOCATION=>true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "Content-Type: application/json",
    "Authorization: Bearer ".$authkey,
  ),
  CURLINFO_HEADER_OUT=>true
));

$response = curl_exec($curl);
var_dump($response);
var_dump(curl_getinfo($curl, CURLINFO_HEADER_OUT));

And I see this:

string(181) "{"error":"unauthorized","type":"https://developer.nest.com/documentation/cloud/error-messages#auth-error","message":"unauthorized","instance":"63d53235-5548-4d4f-b791-360c53baef4a"}"
string(165) "GET / HTTP/1.1
Host: firebase-apiserver14-tah01-iad01.dapi.production.nest.com:9553
Accept: */*
Accept-Encoding: deflate, gzip
Content-Type: application/json

"

It seems like A request to developer-api.nest.com has been redirected to firebase-apiserver14-tah01-iad01.dapi.production.nest.com:9553. OK... But on the subsequent request cURL isn't sending the Authorization header. Curiously, it is sending the Content-type header...

The request fails because it is unauthorized.

Any help appreciated. Cheers

Upvotes: 1

Views: 416

Answers (1)

jptanguay
jptanguay

Reputation: 31

We had a similar problem with Curl not passing the Bearer token to the next URL in the redirection.

We solved the problem by adding the parameter curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, true);

in your case:

...
CURLOPT_UNRESTRICTED_AUTH => true,
...

Upvotes: 0

Related Questions