1

I'm trying to use the code of an app from the IBM website that detects faces in images. I live in Australia and I keep getting this error Unauthorized: Access is denied due to invalid credentials when I call node app.js
This is my code:

function main(params) {
  return new Promise(function (resolve, reject) {
    var res = {};

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

    var url = params.url || 'https://gateway-a.watsonplatform.net/visual-recognition/api' ;
    var use_unauthenticated =  params.use_unauthenticated || false ;

    const visual_recognition = new VisualRecognitionV3({
      'api_key': params.api_key,
      'version_date': '2016-05-20',
      'url' : url,
      'use_unauthenticated': use_unauthenticated
    });

    visual_recognition.detectFaces({'url': params.imageurl}, function(err, res) {
      if (err)
        reject(err);
      else
        resolve(res);
    });
  });
}

const defaultParameters = {
  'api_key': 'API KEY',  
  'imageurl': 'https://www.whitehouse.gov/sites/whitehouse.gov/files/images/' +
         'first-family/44_barack_obama%5B1%5D.jpg',
  'url' : 'https://gateway-a.watsonplatform.net/visual-recognition/api',
  'use_unauthenticated' : true
}

if (require.main === module)
  main(defaultParameters)
    .then((results) => console.log(JSON.stringify(results, null, 2)))
    .catch((error) => console.log(error.message));

What am I doing wrong? Why do I keep getting this error???

Karliene
  • 147
  • 1
  • 3
  • 10

2 Answers2

0
'api_key': 'API KEY',

Have you replaced the API KEY with the appropriate value?

Manglu
  • 10,744
  • 12
  • 44
  • 57
  • Yes, the API key is replaced with the appropriate value. I think I forgot to close the quesiton since I found the answer. use_unauthenticated must be set to false, only then it will work properly. Thanks for taking the time to help me though :) – Karliene May 15 '18 at 07:49
0

In most cases will be because you are using the wrong endpoint. Check which url you should be using against your instance of Watson Visual Recognition. This will be the same place where you got your key.

var url = params.url || 'https://gateway-a.watsonplatform.net/visual-recognition/api' ;

Check what you have in params.url and what url gets set to.

chughts
  • 4,210
  • 2
  • 14
  • 27