IBM Watson Visual Recognition - Access is denied d

2019-07-11 14:15发布

I'm trying to use IBM Watson Visual Recognition tool with nodejs (express). I followed the instruction from the guide, but I can't connect with the tool.

var fs = require('fs');
var VisualRecognitionV3 = require('watson-developer-cloud/visual-recognition/v3');

var visualRecognition = new VisualRecognitionV3({
    version: '2018-03-19',
    api_key: 'api key',
});

var images_file = fs.createReadStream('public/images/fruitbowl.jpg');

var classifier_ids = ["food"];

var params = {
    images_file: images_file,
    classifier_ids: classifier_ids
};

visualRecognition.classify(params, function(err, response) {
    if (err)
        console.log(err);
    else
        var resp = JSON.stringify(response, null, 2)
        console.log(JSON.stringify(response, null, 2))
});

When I run my nodejs app, I got this message

Error: Unauthorized: Access is denied due to invalid credentials.

Does someone know the solution to this authentification problem?

1条回答
Melony?
2楼-- · 2019-07-11 15:05

Your dashboard likely looks like this with your API key listed. If you have a bunch of iam_**** properties, you probably need to authenticate using the iam_apikey instead of api_key.

Watson credentials

Per the Watson Authentication documentation, you should probably be authenticating with code that looks like this:

const fs = require("fs");
const VisualRecognition = require("watson-developer-cloud/visual-recognition/v3");

const vr = new VisualRecognition({
    version: "2018-03-19",
    iam_apikey: "MY_API_KEY" // Instead of api_key
});

const images_file = fs.createReadStream("./fruit.jpg");

vr.classify({
    images_file,
    classifier_ids: ["food"]
}, (err, res) => {
    if (err) {
        throw err;
    }
    console.log(JSON.stringify(res));
});

Using that code on this image: Wikipedia Fruit, yields the following:

{
  "images": [
    {
      "classifiers": [
        {
          "classifier_id": "food",
          "name": "food",
          "classes": [
            {
              "class": "non-food",
              "score": 0.946
            }
          ]
        }
      ],
      "image": "fruit2.jpg"
    }
  ],
  "images_processed": 1,
  "custom_classes": 0
}
查看更多
登录 后发表回答