Can't access IBM Tone Analyzer API?
Asked Answered
J

2

7

I'm trying to use the Tone Analyzer API in a Laravel application. No matter what I try, I always get the same response of {"code":401, "error": "Unauthorized"}. I suspect my issue is that I can't figure out how to pass in the API key, but the official documentation is no help whatsoever because it only contains instructions for using cURL in the command line. My code currently looks like this (though I have tried many many other iterations. If anyone needs me to I can post all the other unsuccessful attempts as well):

$response = Curl::to('https://gateway-wdc.watsonplatform.net/tone-analyzer/api/v3/tone?version=2017-09-21&sentences=false')
        ->withOption('HTTPHEADER', array(
            'Content-Type: application/json',
            'apikey: REDACTED'))
        ->withData(array('text' => $text))
        ->asJson()
        ->post();

I am running Laravel 5.8 and using Ixudra's cURL library. I would prefer if answers made use of this library too but honestly at this point I'm ready to give up and use vanilla PHP anyway so any answers are appreciated.

Ninja edit: I know the problem is not my account / API key, because I have tried to access the API through the command line and it worked just as expected. The issue only arises when trying to access it from Laravel.

Judon answered 30/4, 2019 at 2:0 Comment(0)
I
7

IBM Watson Services uses HTTP Header Authentication in Basic format. Therefore, using curl in the terminal, you should pass the-u or --user flag in the format user:password, or you can also send the Authentication Http Header in pattern: Basic user:password.

By adjusting your code for this second form, you can do it as follows:

$response = Curl::to('https://gateway-wdc.watsonplatform.net/tone-analyzer/api/v3/tone?version=2017-09-21&sentences=false')
        ->withHeader('Content-Type: application/json')
        ->withHeader('Authorization: Basic apikey:YOUR_TOKEN_HERE')
        ->withData(array('text' => $text))
        ->asJson()
        ->post();

Replace YOUR_TOKEN_HERE by your Tone Analyzer API access token.

https://developer.mozilla.org/docs/Web/HTTP/Authentication https://www.ibm.com/support/knowledgecenter/en/SSGMCP_5.3.0/com.ibm.cics.ts.internet.doc/topics/dfhtl2a.html

Hope this helps!

Illuviation answered 2/5, 2019 at 14:21 Comment(3)
Thank you for the answer but it still didn't work :( it still returns the same "401 Unauthorized" message.Judon
Okay wait I think I've got it working actually. I had to replace the second withHeader() statement with ->withOption('USERPWD', 'apikey:[REDACTED]). Still marking this one as the best because you did get me most of the way there.Judon
Thank you! I Wish Success In Your Project!Illuviation
S
3

It's 401 status code which uses for unauthorized access, you need to login first before accessing the API.

I check the docs for this and here is the link, for login to the api before using it tone-analyzer#authentication

With some service instances, you authenticate to the API by using IAM. You can pass either a bearer token in an Authorization header or an API key. Tokens support authenticated requests without embedding service credentials in every call. API keys use basic authentication.

Surety answered 2/5, 2019 at 13:59 Comment(3)
Thank you for the answer, but this is documentation for using cURL in the command line. I stated in the question that I specifically need help with accessing it from PHP. I can access the API just fine from the command line, so I know the issue isn't that I'm not authorized; I just can't figure out how to construct the PHP call.Judon
Jess STJ, I thing you did not get my answer. You can do this with your curl Class or with some pure php curl if uh know about curl or uh can use beautiful library docs.guzzlephp.org/en/stable , its just a http request first you need to login through http request as that documentation said. in return you will get token. and then your every request shoud have this token.Surety
I'm sorry but I still have no idea what you mean. I can't log in because I don't have a username and password, all I have is an API key. IBM even states that using a username and password is now deprecated in favour of using this key.Judon

© 2022 - 2024 — McMap. All rights reserved.