0

I would like to access IBM Tone Analyzer through PHP using CURL, however, I keep getting authorization errors like shown below. The code I use is shown in the code section.

Although looking up numerous help pages and forums, I haven't found a suitable solution. Could someone please help me how to solve this?

Thanks a lot in advance.

With best regards!

This is the code i use:

<?php

//print_r($_POST);

$ch = curl_init();

$file_path = "tone.json";
$url = "https://api.eu-gb.tone-analyzer.watson.cloud.ibm.com/instances/$BLACKENED$/v3/tone?$

$header_args = array(
   'Accept: application/json',
   'Content-Type: application/json',
   'Authorization: Basic apikey:$BLACKENED$'
);

curl_setopt_array($ch,array(CURLOPT_URL => $url, CURLOPT_HEADER => $header_args, CURLOPT_UPLOAD => true, CURLOPT_POST => true, CURLOPT_INFILE => $file_path));

$response = curl_exec($ch);

echo($response);

curl_close($ch);

?>

This is the error i get:

HTTP/1.1 401 Unauthorized strict-transport-security: max-age=31536000; includeSubDomains; WWW-Authenticate: Basic realm="IBM Watson Gateway(Log-in)" Content-Length: 164 Content-Type: application/json x-dp-watson-tran-id: $BLACKENED$ x-request-id: $BLACKENED$ x-global-transaction-id: $BLACKENED$ Server: watson-gateway X-EdgeConnect-MidMile-RTT: 0 X-EdgeConnect-Origin-MEX-Latency: 380 Date: Mon, 06 Dec 2021 21:46:28 GMT Connection: close {"code":401,"more_info":"https://cloud.ibm.com/docs/watson?topic=watson-authorization-error","error":"Unauthorized","trace":"$BLACKENED$"} 1
  • My guess - you will need to supply a better, more fully scoped set of parameters to support your curl request - specifically based around SSL connections. There are a few that should be used whenever you deal with an SSL protected endpoint - such as `CURLOPT_SSL_VERIFYPEER`,`CURLOPT_SSL_VERIFYHOST` and `CURLOPT_CAINFO` where you supply a valid `cacert.pem` file uri. Also - what is `CURLOPT_POS`??? have you truncated the values you send or is that a typo?? – Professor Abronsius Dec 06 '21 at 22:23
  • @ProfessorAbronsius it's not likely an SSL problem. I originally thought the same until I realized that OP just posted the entire HTTP response with no line breaks. The actual reason, given in the body of the response at the end, is an authentication error. – Sammitch Dec 06 '21 at 22:41
  • 1
    The response tells you that it is an authentication error, and even gives you a link to the relevant docs including troubleshooting. https://cloud.ibm.com/docs/watson?topic=watson-authorization-error – Sammitch Dec 06 '21 at 22:41
  • To @ProfessorAbronsius: Thanks for your response. I missed something from my code which i edited now. I do not get an cacert.pem file, but i tried the ssl options. However, this doesn't helped in any way. I see that it is an authentication error, but how can i solve this? – GIS_GIS_GIS Dec 07 '21 at 11:55
  • To @Sammitch: Thanks, I've read this documentation, but it didn't help me so far. You have some advice on how to solve this? – GIS_GIS_GIS Dec 07 '21 at 11:56

0 Answers0