ibmtjbot / tjbot

IBM TJBot
https://ibmtjbot.github.io
Apache License 2.0
481 stars 282 forks source link

VisualRecognitionV3 Authentication Update #98

Closed fadub closed 6 years ago

fadub commented 6 years ago

In tjbot.js the code block starting on line 429 must be updated. When a new Visual Recognition Service is created (as per May 23, 2018) one gets the following credentials.

 {
   "apikey": "API_KEY",
   "iam_apikey_description": "...",
   "iam_apikey_name": "...",
   "iam_role_crn": "...",
   "iam_serviceid_crn": "...",
   "url": "https://gateway.watsonplatform.net/visual-recognition/api"
 }

And the apikey has to be used as follows (see API Reference):

var visualRecognition = new VisualRecognitionV3({
    version: '{version}',
    iam_apikey: '{iam_api_key}'
  });

Currently the code starting on line 435 in tjbot.js looks like this:

assert(credentials.hasOwnProperty('api_key'), "credentials for the " + service + " service missing 'api_key'");

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

// see this note about authentication:
// https://www.ibm.com/watson/developercloud/visual-recognition/api/v3/node.html?node#authentication
if (credentials['api_key'] != undefined) {
    this._visualRecognition = new VisualRecognitionV3({
        api_key: credentials['api_key'],
        version: '2018-03-19'
    });
} else if (credentials['iam_apikey'] != undefined) {
    this._visualRecognition = new VisualRecognitionV3({
        api_key: credentials['iam_apikey'],
        version: '2018-03-19'
    });
} else {
    throw new Error(
        'No authentication credentials specified for visual_recognition service.');
}

I suggest to change/add the following (another else if):

assert((credentials.hasOwnProperty('api_key') || credentials.hasOwnProperty('apikey')) , "credentials for the " + service + " service missing 'api_key' / 'apikey'");

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

// see this note about authentication:
// https://www.ibm.com/watson/developercloud/visual-recognition/api/v3/node.html?node#authentication
if (credentials['api_key'] != undefined) {
    this._visualRecognition = new VisualRecognitionV3({
        api_key: credentials['api_key'],
        version: '2018-03-19'
    });
} else if (credentials['iam_apikey'] != undefined) {
    this._visualRecognition = new VisualRecognitionV3({
        api_key: credentials['iam_apikey'],
        version: '2018-03-19'
    });
} else if (credentials['apikey'] != undefined) {
    this._visualRecognition = new VisualRecognitionV3({
        iam_apikey: credentials['apikey'],
        version: '2018-03-19'
    });
} else {
    throw new Error(
        'No authentication credentials specified for visual_recognition service.');
}

The current TJBot code throws an error when used with a Visual Recognition Service created after May 23, 2018.

jweisz commented 6 years ago

This has been updated in tjbotlib 1.5. Thanks.