Search code examples
ibm-cloudibm-watsonlanguage-translation

WatsonApiException: Error: Unauthorized: Access is denied due to invalid credentials , Code: 401


I'm trying to connect to a Language Translator instance as per the docs:

!pip install --upgrade "watson-developer-cloud>=1.4.0"

import json
from watson_developer_cloud import LanguageTranslatorV2

translator = LanguageTranslatorV2(
    iam_api_key='***my_api_key***',
    url='https://gateway.watsonplatform.net/language-translator/api'
)

models = translator.list_models()
print(json.dumps(models, indent=2))

However, this results in:

WatsonApiException: Error: Unauthorized: Access is denied due to invalid credentials , Code: 401 , Information: {'description': 'No credentials supplied.'} , X-dp-watson-tran-id: xxxxxx , X-global-transaction-id: xxxxx

I found the API Key in the service credentials. I tried both apikey and iam_apikey_name but both result in the same error:

{
 “apikey”: “***my_api_key***”,
 “iam_apikey_description”: “Auto generated apikey during resource-key operation for Instance - crn:v1:bluemix:public:language-translator:us-south:xxxxxx::“,
 “iam_apikey_name”: “auto-generated-apikey-xxxxxx",
 “iam_role_crn”: “crn:v1:bluemix:public:iam::::serviceRole:Manager”,
 “iam_serviceid_crn”: “crn:v1:bluemix:public:iam-identity::xxxxxx::serviceid:ServiceId-xxxxxx",
 “url”: “https://gateway.watsonplatform.net/language-translator/api”
}

I also tried creating a Platform API Key for IBM Cloud. Same error.

I also tried creating an access token:

%%bash
curl -k -X POST \
  --header "Content-Type: application/x-www-form-urlencoded" \
  --header "Accept: application/json" \
  --data-urlencode "grant_type=urn:ibm:params:oauth:grant-type:apikey" \
  --data-urlencode "apikey=**myapikey**" \
  "https://iam.bluemix.net/identity/token"

Then using the access token returned by curl:

translator = LanguageTranslatorV2(iam_access_token='**generated_access_token**')

Same error :(

I think this issue is similar to this question posted for VisualRecognition, but in this case for LanguageTranslator.


Solution

  • Language Translator now has a V3 version of the service. Perhaps you created a V3 service instance? If so, you need to use the LanguageTranslatorV3 module in the Watson Python SDK.