【问题标题】:Not able to upload a file to slack via web-api无法通过 web-api 将文件上传到 slack
【发布时间】:2017-07-27 07:12:47
【问题描述】:

我正在尝试使用https://api.slack.com/methods/files.upload 提供的 Web-API 将文件 sn-p 上传到 slack 中的频道

 payload_channel_caller = {'token': 'xoxpxxxxb1d8529c', 'channel':'C1PJ17FFT', 'file': "/home/nsingh/slack_shift/user_list" ,'title':'Shifters'}

    print "This is a test"
    requests.post('https://slack.com/api/files.upload', data=payload_channel_caller)

但上面的代码无法上传文件,实际上它运行正常,没有错误。

不知道这里出了什么问题。

谁能帮帮我

【问题讨论】:

  • 我几乎可以肯定您必须实际发送文件对象而不是文件路径,例如'file': open("/path/to/file", "rb")
  • 有什么反应? (r.content)

标签: python python-2.7 python-requests slack slack-api


【解决方案1】:

修改点:

在您的脚本中,您要上传的文件未被读取。当您的脚本运行时,将检索以下响应。

{"ok":false,"error":"no_file_data"}

此时,Slack 的状态码是 200。所以没有错误发生。以上反映的脚本如下。

修改后的脚本:

import requests
uploadfile = "/home/nsingh/slack_shift/user_list"  # Please input the filename with path that you want to upload.
with open(uploadfile, 'rb') as f:
    param = {
        'token': 'xoxpxxxxb1d8529c',
        'channels': 'C1PJ17FFT',
        'title': 'Shifters'
    }
    r = requests.post(
        "https://slack.com/api/files.upload",
        params=param,
        files={'file': f}
    )
    print r.text

如果我误解了你的问题,我很抱歉。

【讨论】:

  • 上面的代码仍然没有上传,但运行良好
  • 我已经确认这个脚本工作正常。我可以向您询问错误消息和/或响应消息吗?你要上传什么文件?
  • 哦,是的.. 抱歉刚刚检查了一下,它正在使用我的令牌,所以我无法收到通知
  • @Kittystone 听到这个我松了一口气。谢谢。
猜你喜欢
  • 1970-01-01
  • 2018-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多