IBM Watson Personality Insights 令牌身份验证 Python
IBM Watson Personality Insights token Authentication Python
谁能告诉我生成和使用 IBM 个性洞察令牌的过程。
Watson Personality Insights token
为此,您需要使用 Python 对令牌使用 HTTP GET 请求并获取令牌。
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)
观察:"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."
查看关于 Tokens with IBM Watson 的官方文档here。
谁能告诉我生成和使用 IBM 个性洞察令牌的过程。
Watson Personality Insights token
为此,您需要使用 Python 对令牌使用 HTTP GET 请求并获取令牌。
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)
观察:"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."
查看关于 Tokens with IBM Watson 的官方文档here。