我正在通过python库slackclient进行松散的api调用,这是一个关于slack api的包装器。但是,在某些情况下,我还需要使用url和get / post方法进行常规的api调用。我试图通过我的机器人与另一个用户打开直接消息通道。文档 - https://api.slack.com/methods/im.open表示"将这些参数作为application / x-www-form-urlencoded查询字符串或POST正文的一部分呈现。 application / json目前不被接受。"
现在在python中,我可以写,
url = 'https://slack.com/api/im.open'
headers = {'content-type':'x-www-form-urlencoded'}
data = {'token':BOT_TOKEN, 'user':user_id, 'include_locale':'true','return_im':'true'}
r= requests.post(url,headers,data )
print r.text
我得到的消息是{"ok":false,"error":"not_authed"}
我知道这条消息是"没有兑现"虽然我使用我的机器人令牌和另一个用户ID,但我的预感是我以错误的格式发送请求,因为我只是以某种方式阅读文档。我不确定如何准确发送这些请求。
任何帮助?
答案 0 :(得分:1)
requests.post
中的第二个参数用于data
,因此在您的请求中,您实际上发布了headers
字典。如果要使用headers
,可以按名称传递参数。
r= requests.post(url, data, headers=headers)
但是在这种情况下不需要这样做,因为'x-www-form-urlencoded'
是发布表单数据时的默认值。
答案 1 :(得分:1)
因为Content-Type标头是x-www-form-urlencoded
以字典的形式发送数据不起作用。你可以尝试这样的事情。
import requests
url = 'https://slack.com/api/im.open'
headers = {'content-type': 'x-www-form-urlencoded'}
data = [
('token', BOT_TOKEN),
('user', user_id),
('include_locale', 'true'),
('return_im', 'true')
]
r = requests.post(url, data, **headers)
print r.text