Yipin
Yipin

Reputation: 173

How to find the user id and password for IBM API?

I am a beginner for IBM API. I just launched an IBM Natural Language Understanding service. However, what I got is the API key instead of user id and passwords. Like this:

{
  "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"
}

In the example below, it shows I need a user id and a password. Where can I find them? Thanks!

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

Upvotes: 2

Views: 2103

Answers (3)

German Attanasio
German Attanasio

Reputation: 23653

We added support for IAM in version 1.3.3. Always make sure you are using the latest version.

With IAM you will replace username and password with an iam_apikey parameter from the apikey credential field.

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

Upvotes: 1

cathaldi
cathaldi

Reputation: 161

This is all explained in the getting started tutorial from the instance.

  1. Click Show to view your credentials.

  2. Copy the username, password, and url values.

Important: The tutorial uses service instance credentials to authenticate to the Natural Language Understanding service. In some regions, new service instances instead use IBM® Cloud Identity and Access Management (IAM) tokens for authentication. Authenticate by using the approach that is right for your region and service instance.

They mention differing authentication types by region - but they don't really specify which regions use which type.

It is pointed out in the release notes

29 May 2018

The service now supports a new API authentication process for service instances created in Sydney (au-syd). IBM® Cloud is in the process of migrating to token-based Identity and Access Management (IAM) authentication. IAM uses access tokens rather than service credentials for authentication with a service.

As of 29th of May only newly created instance in Sydney (au-syd) use a different authentication method. I'm not sure if there is a better way to find this information out besides crawling through release notes chronologically.


So if your instance was created in the Sydney (au-syd) region after the 28th May 2018 or other regions have since been moved over to this system you'll have to generate a token and pass it through instead.

Using basic auth to intially get the token

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"

Then using the token from response for further API calls.

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

Just keep in mind that you will need to refresh the token periodically.

Upvotes: 1

nitind
nitind

Reputation: 20003

Looks like your app needs to use the API key there to request a bearer token from the Identity and Access Manager, according to the instructions at https://github.com/watson-developer-cloud/node-sdk/blob/master/README.md#authentication and https://console.bluemix.net/docs/services/watson/getting-started-iam.html#iam .

Upvotes: 0

Related Questions