POST到Quire API的问题

问题描述 投票:0回答:2

我一直在使用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 quire-api
2个回答
0
投票

我不熟悉python请求API,因此我不了解默认标头。但是,您好像错过了以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

0
投票

@ cor3000提供的答案表明,帖子数据应作为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

© www.soinside.com 2019 - 2024. All rights reserved.