机智AI留言API
Wit AI message API
谁能告诉我如何向 Wit.ai 消息 api 发出请求。我正在为我的代码苦苦挣扎。
import requests
import json
import sys
from wit import Wit
# Wit speech API endpoint
API_ENDPOINT = 'https://api.wit.ai/message'
q='who are you'
# Wit.ai api access token
wit_access_token = 'B3GHXHLTXIASO7S4KY7UC65LMSTCDEHK'
# defining headers for HTTP request
headers = {'authorization': 'Bearer ' + wit_access_token}
# making an HTTP post request
resp = requests.post(API_ENDPOINT, headers = headers,data = {'q':'who are you'})
# converting response content to JSON format
data = json.loads(resp.content)
print(data)
我要回复这个:
{u'code': u'json-parse', u'error': u'Invalid JSON'}
/message
endpoint of Wit API accepts only GET
method and expects the query parameters in URL (not data
in request body). The requests
library will correct the case on your lowercase Authorization
header field, but it's a good practice to write it according to the standard。此外,可以使用 Response
上的 json()
方法解码 JSON 响应。
所有这些:
import requests
API_ENDPOINT = 'https://api.wit.ai/message'
WIT_ACCESS_TOKEN = 'B3GHXHLTXIASO7S4KY7UC65LMSTCDEHK'
headers = {'Authorization': 'Bearer {}'.format(WIT_ACCESS_TOKEN)}
query = {'q': 'who are you'}
resp = requests.get(API_ENDPOINT, headers=headers, params=query)
data = resp.json()
print(data)
但是请注意,Wit 有一个 Python library,它抽象了所有这些低级细节,并使您的代码更加简单易读。使用它。
看起来像这样(example 来自文档):
from wit import Wit
client = Wit(access_token=WIT_ACCESS_TOKEN)
resp = client.message('what is the weather in London?')
print('Yay, got Wit.ai response: ' + str(resp))
谁能告诉我如何向 Wit.ai 消息 api 发出请求。我正在为我的代码苦苦挣扎。
import requests
import json
import sys
from wit import Wit
# Wit speech API endpoint
API_ENDPOINT = 'https://api.wit.ai/message'
q='who are you'
# Wit.ai api access token
wit_access_token = 'B3GHXHLTXIASO7S4KY7UC65LMSTCDEHK'
# defining headers for HTTP request
headers = {'authorization': 'Bearer ' + wit_access_token}
# making an HTTP post request
resp = requests.post(API_ENDPOINT, headers = headers,data = {'q':'who are you'})
# converting response content to JSON format
data = json.loads(resp.content)
print(data)
我要回复这个:
{u'code': u'json-parse', u'error': u'Invalid JSON'}
/message
endpoint of Wit API accepts only GET
method and expects the query parameters in URL (not data
in request body). The requests
library will correct the case on your lowercase Authorization
header field, but it's a good practice to write it according to the standard。此外,可以使用 Response
上的 json()
方法解码 JSON 响应。
所有这些:
import requests
API_ENDPOINT = 'https://api.wit.ai/message'
WIT_ACCESS_TOKEN = 'B3GHXHLTXIASO7S4KY7UC65LMSTCDEHK'
headers = {'Authorization': 'Bearer {}'.format(WIT_ACCESS_TOKEN)}
query = {'q': 'who are you'}
resp = requests.get(API_ENDPOINT, headers=headers, params=query)
data = resp.json()
print(data)
但是请注意,Wit 有一个 Python library,它抽象了所有这些低级细节,并使您的代码更加简单易读。使用它。
看起来像这样(example 来自文档):
from wit import Wit
client = Wit(access_token=WIT_ACCESS_TOKEN)
resp = client.message('what is the weather in London?')
print('Yay, got Wit.ai response: ' + str(resp))