watson-developer-cloud / node-sdk

:comet: Node.js library to access IBM Watson services.
https://www.npmjs.com/package/ibm-watson
Apache License 2.0
1.48k stars 669 forks source link

[visual-recognition-service] cURL works fine but SDK gets error for the same settings #716

Closed KerimG closed 6 years ago

KerimG commented 6 years ago

I created a visual recognition service and tested it with cURL like so:

curl -u "apikey:{apikey}" "https://gateway.watsonplatform.net/visual-recognition/api/v3/classify?url={image-url}&version=2018-03-19&classifier_ids=DefaultCustomModel_325422001" and this works perfectly fine.

When I try to do the same via:

var visual_recognition = new VisualRecognitionV3({
  apikey: "{apikey}",
  version: "2018-03-19",
  url: "https://gateway.watsonplatform.net/visual-recognition/api/"
});

visual_recognition.classify(
        {
          classifier_ids: ["DefaultCustomModel_325422001"],
          image_file: {image file}
        },
        function(err, response) {
          console.log(err, response);
        }
      );

I get the following error:

{ Error: Unauthorized: Access is denied due to invalid credentials.
    at Request._callback (/project/node_modules/watson-developer-cloud/lib/requestwrapper.js:102:21)
    at Request.self.callback (/project/node_modules/watson-developer-cloud/node_modules/request/request.js:185:22)
    at emitTwo (events.js:106:13)
    at Request.emit (events.js:191:7)
    at Request.<anonymous> (/project/node_modules/watson-developer-cloud/node_modules/request/request.js:1157:10)
    at emitOne (events.js:96:13)
    at Request.emit (events.js:188:7)
    at Gunzip.<anonymous> (/project/node_modules/watson-developer-cloud/node_modules/request/request.js:1079:12)
    at Gunzip.g (events.js:292:16)
    at emitNone (events.js:91:20)
  code: 401,
  error: 'Unauthorized',
  body: 'Unauthorized',
  'x-global-transaction-id': '7ecac92c5b1a89bb17f3b733' }

Expected behavior: Get response object containing classification.

Actual behavior: Receive unauthorized error.

Node version: 6.11.5

SDK version: 3.4.5dd

What I found out is that the cURL request above uses GET, while the SDK uses POST. I'm not sure whether that's causing the issue but I can't really find any difference.

anweshan commented 6 years ago

Does your dashboard show anything like iam_description? Your error is just that the parameter isn't called apikey

If you do not see anything related to iam_description or iam on your credentials:

If you are using IAM authentication and see things like iam on your credentials page:

andycitron commented 6 years ago

I'm having a similar issue. According to the current docs for this package, when using a vision service that was created recently, the service should be created with this template:
var visual_recognition = new VisualRecognitionV3({ url: config.VisionUrl, version: config.VisionVersion, iam_apikey: config.VisionKey });

But when running this, the installed v3 code throws an exception because it wants api_key to be specified. The same code also don't like 'version' to be specified.

I've tried modifying the v3 code to work around the problem, but have not succeeded.

KerimG commented 6 years ago

@anweshan

I do see iam_description but I don't have an iam_key. Using api_key returns a similar message, going "invalid api key"

germanattanasio commented 6 years ago

@KerimG you need to set the iam_apikey parameter in the constructor using the apikey value from the credentials in the IBM Cloud console.

var visual_recognition = new VisualRecognitionV3({
  iam_apikey: "{apikey}", // change here
  version: "2018-03-19",
  url: "https://gateway.watsonplatform.net/visual-recognition/api/"
});
KerimG commented 6 years ago

I think you missed my last message.

I have no iam_apikeyin my credentials and neither api_keynor apikey work. I was out of the country and had no chance to continue on this issue, yet.

Imho this was closed prematurely, especially considering I don't seem to be the only one having this or similar to this issue.

germanattanasio commented 6 years ago

@KerimG can you show me what you have in the credentials object? I think you have an apikey field. If you use that field as iam_apikey it will work. See https://github.com/watson-developer-cloud/node-sdk#supplying-the-iam-api-key

Make sure you override the url parameter also.

anweshan commented 6 years ago

@KerimG We closed this issue because we were unable to duplicate the issue and it appears that it should be fixed if you correctly pass in your api key like we suggested above. I re-opened the issue because you asked, but will close if there is no more activity on this issue. Can you verify that the suggestion worked?

KerimG commented 6 years ago

@anweshan thank you for re-opening.

Creating an instance as follows:

var visual_recognition = new VisualRecognitionV3({
  iam_apikey: "{apikey}",
  version: "2018-03-19",
  url: "https://gateway.watsonplatform.net/visual-recognition/api/"
});

returns this error:

Error: {"context":{"requestId":"21722353","requestType":"incoming.Identity_Token","userAgent":"Go-http-client/1.1","clientIp":"176.95.130.239","url":"https://iam-id-2.eu-gb.bluemix.net","instanceId":"iamid-9fd57f7dc-rrv75","threadId":"7307d2","host":"iamid-9fd57f7dc-rrv75","startTime":"21.06.2018 07:43:11:796 UTC","endTime":"21.06.2018 07:43:11:861 UTC","elapsedTime":"65","locale":"en_US","clusterName":"iam-id-prlon04-6r67"},"errorCode":"BXNIM0415E","errorMessage":"Provided API key could not be found"}

I am pretty sure that that api key does exist. I will create another set of credential just to make sure. Does the role of the credential matter? When I try to create credentials with the role "writer" , I get a 400 error pop-up.

KerimG commented 6 years ago

I created a new set of credentials and had the same issue.

I'm currently using the free plan for visual recognition, could it be that I reached my 1000 API calls limit? I don't feel like I did and the error message doesn't mention anything regarding that but is it possible? I'll create a new account just to test.

germanattanasio commented 6 years ago

Looks like the API key isn't valid. Based on the error message: Provided API key could not be found

Here is a set of credentials that I just got after creating an instance:

{
  "apikey": "N5msBe7aLCK05w2HSwbrr7iZyMftHMY9iC8-aMVVk804",
  "iam_apikey_description": "Auto generated apikey during resource-key operation for Instance - crn:v1:staging:public:conversation:us-south:a/a2b41967b8284f612fd4b7fd00297819:49014c11-282a-4a74-87ab-8b3dc0777d9b::",
  "iam_apikey_name": "auto-generated-apikey-933a2344-2baa-491c-8ed8-9a5ff1238ed3",
  "iam_role_crn": "crn:v1:bluemix:public:iam::::serviceRole:Manager",
  "iam_serviceid_crn": "crn:v1:staging:public:iam-identity::a/a2b41967b8284f612fd4b7fd00297819::serviceid:ServiceId-de9cbe30-b8ad-40df-b518-884fef8ec713",
  "url": "https://gateway.watsonplatform.net/visual-recognition/api"
}

My API Key is:

N5msBe7aLCK05w2HSwbrr7iZyMftHMY9iC8-aMVVk804

To use the SDK I do:

var visual_recognition = new VisualRecognitionV3({
  iam_apikey: "N5msBe7aLCK05w2HSwbrr7iZyMftHMY9iC8-aMVVk804",
  version: "2018-03-19",
  url: "https://gateway.watsonplatform.net/visual-recognition/api"
});

visual_recognition.classify({ }, (err, response) => {
  console.log(err, response);
});
KerimG commented 6 years ago

I think it's an account issue. I'm not sure why. I created a new account and things work there.

Thanks a lot for your help.