如何找到IBM API的用户标识和密码?

时间:2018-06-19 22:36:54

标签: ibm-cloud

我是IBM API的初学者。我刚刚启动了IBM自然语言理解服务。但是,我得到的是API密钥,而不是用户ID和密码。像这样:

{
  "apikey": "••••••••••••••••••••••••••••••••••••••••••••",
  "iam_apikey_description": "Auto generated apikey during resource-key operation for Instance - crn:v1:bluemix:public:natural-language-understanding:us-east:a/6514bcdaafbc465498a244edb484cbe5:53e5f23b-f255-4d6c-b48d-cfce09c975b1::",
  "iam_apikey_name": "auto-generated-apikey-51f2d016-d3ec-46bc-8be7-496ae621983d",
  "iam_role_crn": "crn:v1:bluemix:public:iam::::serviceRole:Manager",
  "iam_serviceid_crn": "crn:v1:bluemix:public:iam-identity::a/6514bcdaafbc465498a244edb484cbe5::serviceid:ServiceId-d83a34de-5860-443a-817a-b3cb3fb44e2a",
  "url": "https://gateway-wdc.watsonplatform.net/natural-language-understanding/api"
}

在下面的示例中,它显示我需要一个用户ID和一个密码。在哪里可以找到它们?谢谢!

import json
from watson_developer_cloud import NaturalLanguageUnderstandingV1
from watson_developer_cloud.natural_language_understanding_v1 \
import Features, EntitiesOptions, KeywordsOptions

natural_language_understanding = NaturalLanguageUnderstandingV1(
  username='username',
  password='password',
  version='2018-03-16')

 response = natural_language_understanding.analyze(
  text='IBM is an American multinational technology company '
   'headquartered in Armonk, New York, United States, '
   'with operations in over 170 countries.',
 features=Features(
entities=EntitiesOptions(
  emotion=True,
  sentiment=True,
  limit=2),
keywords=KeywordsOptions(
  emotion=True,
  sentiment=True,
  limit=2)))

print(json.dumps(response, indent=2))

3 个答案:

答案 0 :(得分:1)

在实例的getting started tutorial中对此进行了全部解释。

  
      
  1. 单击“显示”以查看您的凭据。

  2.   
  3. 复制用户名,密码和url值。

  4.   
     

重要提示:本教程使用服务实例凭据来   向自然语言理解服务认证。在一些   区域,新服务实例改为使用IBM®Cloud Identity和   用于身份验证的访问管理(IAM)令牌。通过验证   使用适合您的区域和服务实例的方法。

他们提到了不同地区的身份验证类型-但是他们并没有真正指定哪个地区使用哪种类型。

release notes

中指出
  

2018年5月29日

     

服务现在支持服务的新API身份验证过程   在悉尼(au-syd)中创建的实例。 IBM®Cloud正在进行   迁移到基于令牌的身份和访问管理(IAM)   身份验证。 IAM使用访问令牌而不是服务凭证   进行服务身份验证。

截至5月29日,只有悉尼(au-syd)中新创建的实例使用其他身份验证方法。除了按时间顺序浏览发行说明以外,我不确定是否有更好的方法来查找此信息。


因此,如果您的实例是在2018年5月28日以后在悉尼(au-syd)地区创建的,或者其他地区已移至该系统,则您必须{{3} }。

使用基本身份验证最初获取令牌

curl -k -X POST \
  --header "Authorization: Basic Yng6Yng=" \
  --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={api_key}" \
  "https://iam.bluemix.net/identity/token"

然后将响应中的令牌用于进一步的API调用。

curl -X GET \
--header "Authorization: Bearer {token}" \
"https://gateway.watsonplatform.net/discovery/api/v1/environments?version=2017-11-07"

请记住,您将需要定期刷新令牌。

答案 1 :(得分:1)

我们在版本1.3.3中添加了对IAM的支持。始终确保您使用的是最新版本。

使用IAM,您将使用username凭据字段中的password参数替换iam_apikeyapikey

import json
from watson_developer_cloud import NaturalLanguageUnderstandingV1
from watson_developer_cloud.natural_language_understanding_v1 \
import Features, EntitiesOptions, KeywordsOptions

natural_language_understanding = NaturalLanguageUnderstandingV1(
  iam_apikey='the apikey value from your question',
  url='https://gateway.watsonplatform.net/natural-language-understanding/api',
  version='2018-03-16')

 response = natural_language_understanding.analyze(
  text='IBM is an American multinational technology company '
   'headquartered in Armonk, New York, United States, '
   'with operations in over 170 countries.',
 features=Features(
entities=EntitiesOptions(
  emotion=True,
  sentiment=True,
  limit=2),
keywords=KeywordsOptions(
  emotion=True,
  sentiment=True,
  limit=2)))

print(json.dumps(response, indent=2))

答案 2 :(得分:0)

根据https://github.com/watson-developer-cloud/node-sdk/blob/master/README.md#authenticationhttps://console.bluemix.net/docs/services/watson/getting-started-iam.html#iam的指示,您的应用可能需要使用那里的API密钥来向身份和访问管理器请求承载令牌。