有人能告诉我如何向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'}发布于 2017-07-16 23:07:43
Wit的/message端点只接受GET方法,并期望GET中的查询参数(请求体中不是data )。requests库将更正小写Authorization头字段中的大小写,但是根据标准编写它是一个很好的实践。另外,JSON响应可以通过json()方法在Response上获取解码。
在所有这些情况下:
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库,它抽象了所有这些低级别的细节,并使您的代码更简单、更易于阅读。使用它。
它应该是这样的(文档中的示例):
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))https://stackoverflow.com/questions/45133379
复制相似问题