POST 到 Quire API 的问题

Problems with POST to Quire API

我一直在使用 python 玩 Quire API,虽然 GET 调用工作正常,但我无法进行任何成功的 POST 调用。我收到 400 错误:错误请求。对于我可能做错了什么的任何提示,我将不胜感激。

下面是相关的代码片段:

AUTH_ENDPOINT = 'https://quire.io/oauth/token'
API_ENDPOINT = 'https://quire.io/api'

data = {
    'grant_type' : 'refresh_token',
    'refresh_token' : 'my_refresh_code',
    'client_id' : 'my_client_id',
    'client_secret' : 'my_client_secret'
}

r = requests.post(url=AUTH_ENDPOINT, data=data)
response = json.loads(r.text)
access_token = response['access_token']
headers = {'Authorization' : 'Bearer {token}'.format(token=access_token)}

# This works fine
r = requests.get(url=API_ENDPOINT + '/user/id/me', headers=headers)
user = json.loads(r.text)
print(user)

# This doesn't work
task_oid = 'my_task_oid'

data = {
    'description' : 'Test Comment'
}

r = requests.post(
    url=API_ENDPOINT + '/comment/' + task_oid,
    data=data,
    headers=headers,
)

我不熟悉 python 请求 API,所以我不知道默认值 headers。 但是,您似乎错过了将请求数据作为 JSON 字符串发送:

这里是 java 脚本对我有用的东西:

uri: '/comment/my_task_oid',
method: 'POST',
body: '{"description":"hello comment"}'

也许它对 python 也有帮助。

还有一个curl例子:

curl -X POST -H 'Authorization: Bearer my_access_token' -d "{\"description\" : \"a test comment\"}" https://quire.io/api/comment/my_task_oid

@cor3000 提供的答案暗示 post 数据应作为 JSON 传递。我测试了一下,确实有效。这里需要修改 POST 请求:

r = requests.post(
    url=API_ENDPOINT + '/comment/' + task_oid,
    data=json.dumps(data),
    headers=headers,
)

或者你也可以这样做:

r = requests.post(
    url=API_ENDPOINT + '/comment/' + task_oid,
    json=data,
    headers=headers,
)

请求 文档中的更多详细信息:https://requests.kennethreitz.org/en/master/user/quickstart/#more-complicated-post-requests