0

I'm trying to generate an ACCESS_TOKEN, then use that token to make a API call. When I try to execute the code below:

$curl = curl_init();
$ID = "5644565-564s6d5f4564-564f5sd645fd";
$SECRET = "256545fsd-456sdfs-546ds6ffds";
$AUDIANCE = "DTY";
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://my-url.com/token",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_SSL_VERIFYHOST => 0,
  CURLOPT_SSL_VERIFYPEER => 0,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "grant_type=client_credentials&client_id=$ID&client_secret=$SECRET&audience=$AUDIANCE",
  CURLOPT_HTTPHEADER => array(
    "content-type: application/x-www-form-urlencoded"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

I get this message:

{
  "message": "Invalid Authorization header"
}

Can someone help me?

I use Insomnia to Generate the TOKEN. you can see the Token générated from Insomnia Software. Now how can i get tis Token in my PHP script. See Insomnia Screen

1 Answers1

0

You are using the TOKEN as URL which is not correct. You need to specify the api URL in that field

  • I have edited the code. Can you please look at it and see what is happened ? – Eburnilive Eburnie Sep 25 '20 at 09:09
  • which URL are you trying to access? CURLOPT_URL must have the address you are trying to access to get the info (API url) What service are you trying to integrate ? – dragos.nicolae Sep 25 '20 at 09:45
  • I have the correct URL, i just hide the URL in CURLOPT_URL. – Eburnilive Eburnie Sep 25 '20 at 10:23
  • if you have the correct URL (the service provider URL and not your own URL) then you will need to check: if the token must be as part of the URL - as you have it right now, and how the data is sent. Right now I can see you are using the data as a string in CURLOPT_POSTFIELDS but above you specified to send through POST method - so make sure if the string should be sent with POST or GET (and GET most probably). – dragos.nicolae Sep 25 '20 at 10:35
  • if the API is not secret you should provide the URL to hava a look at their documentation and maybe we will be able to provide you proper help. – dragos.nicolae Sep 25 '20 at 10:39
  • In the documentation it must be sent with POST. I use insomnia to generate the script. In Insomnia software, i can see the TOKEN generated, but in script i don't know how to get it. – Eburnilive Eburnie Sep 25 '20 at 10:42
  • I'm not familiar with this software. However, you are sending a string which will be used to get parameters with $_GET on the other server. So you have two options: make sure you need to send data through POST and in this case make an array with the data (ID, SECRET, AUDIENCE) or, change the method to GET. It's more than sure that the string you are sending is processed with $_GET – dragos.nicolae Sep 25 '20 at 10:58
  • Changed to GET i received this error: {"timestamp":1601031885057,"status":405,"error":"Method Not Allowed","exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method 'GET' not supported","path":"/api-authorization/api/v2/oauth/access-token"} – Eburnilive Eburnie Sep 25 '20 at 11:05
  • In this case send them with POST and change the data type you send according to their documentation – dragos.nicolae Sep 25 '20 at 11:09