Can anyone please let me know the process to generate and use the token for IBM personality insights.
Asked
Active
Viewed 308 times
1 Answers
0
For this, you need to use HTTP GET request to the token with Python and get the token.
one.py:
def generateToken(username, password):
r = requests.get("https://gateway.watsonplatform.net/authorization/api/v1/token?url=https://gateway.watsonplatform.net/personality-insights/api", auth=(username, password))
if r.status_code == requests.codes.ok:
return r.text
def personalityRequest(text, token):
base_url='https://gateway.watsonplatform.net/etc/etc....'
headers = {'X-Watson-Authorization-Token': token, 'Content-Type': 'yourContextType'}
r = requests.post(base_url, headers=headers, data={'body': text})
return r.text
two.py:
token = one.generateToken()
ret = one.personalityRequest("your Text analyze...", token)
print(ret)
Obs.: "Tokens have a time to live (TTL) of one hour, after which you can no longer use them to establish a connection with the service. Existing connections already established with the token are unaffected by the timeout. An attempt to pass an expired or invalid token elicits an HTTP 401 Unauthorized status code from DataPower. Your application code needs to be prepared to refresh the token in response to this return code."
See the official documentation about Tokens with IBM Watson here.
See the official reference about using Authorization inside the SDK here.

Sayuri Mizuguchi
- 5,250
- 3
- 26
- 53
-
Hi - Thank you. this worked. But the thing is when this is in a loop for example I want to trigger Watson request in a for loop for 80 times with the same (username,password) or token. it gives me SSL error. – Charan Aug 18 '17 at 09:44
-
You need to open other question for this. – Sayuri Mizuguchi Aug 18 '17 at 11:56
-
The question is already added. PFB https://stackoverflow.com/questions/45606896/watson-personality-insights-token?rq=1 – Charan Aug 21 '17 at 05:51
-
https://stackoverflow.com/questions/45606896/watson-personality-insights-token?rq=1 – Charan Aug 21 '17 at 05:52
-
The token is generated using the HTTP GET but still throws SSL error – Charan Aug 21 '17 at 05:52
-
Can you edit the question with your current code? I'll try to help you, and if I can't probably one IBM Professional will help you. I can help you to generate your token with this question, but, with the loop, another question is needs – Sayuri Mizuguchi Aug 21 '17 at 12:19
-
New question https://stackoverflow.com/questions/45809208/ssl-error-ibm-watson-personality-insights-python – Charan Aug 22 '17 at 05:01