WatsonApiException:错误:未经授权:由于凭据无效,访问被拒绝,代码:401

时间:2018-06-20 09:36:25

标签: ibm-cloud ibm-watson language-translation

我正在尝试按照docs连接到Language Translator实例:

!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))

但是,结果是:

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

我在服务凭证中找到了API密钥。我同时尝试了apikeyiam_apikey_name,但都导致了相同的错误:

{
 “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”
}

我还尝试为IBM Cloud创建平台API密钥。同样的错误。

我还尝试创建访问令牌:

%%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"

然后使用curl返回的访问令牌:

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

相同的错误:(

我认为此问题类似于为VisualRecognition发布的question,但在此情况下为LanguageTranslator。

1 个答案:

答案 0 :(得分:2)

语言翻译器现在具有该服务的V3版本。也许您创建了V3服务实例?如果是这样,则需要使用Watson Python SDK中的LanguageTranslatorV3模块。