![](/img/trans.png)
[英]Not able to upload/download files from Storage containers using python code
[英]Not able to upload file using slack api files.upload
這個問題可能看起來重復,但我嘗試了很多但沒有成功。 我正在嘗試使用https://slack.com/api/files.upload API 上傳 html 文件,但我總是遇到以下錯誤。 響應 {'ok': False, 'error': 'no_file_data'}
我瀏覽了文檔 [鏈接] https://api.slack.com/methods/files.upload並嘗試了不同的選項,但仍然得到相同的響應 {'ok': False, 'error': 'no_file_data' }
我也在堆棧溢出中看到了許多類似的問題,但沒有一個解決了問題。 [鏈接] 使用 Slack API 上傳時出現 no_file_data 錯誤[鏈接] 如何使用 file.upload 和請求將文件上傳到 slack
下面是我的代碼。
import requests
def post_reports_to_slack(html_report):
"""
"""
url = "https://slack.com/api/files.upload"
# my_file = {html_report, open(html_report, 'rb'), 'html'}
data = {
"token": bot_user_token,
"channels": channel_name,
"file": html_report,
"filetype": "html"
}
# data = "token=" + bot_user_token + \
# "&channels=" + channel_name +\
# "&file=" + html_report + "&filetype=" + "html"
response = requests.post(
url=url, data=data,
headers={"Content-Type": "application/x-www-form-urlencoded"})
print("response", response)
print(response.json())
if response.status_code == 200:
print("successfully completed post_reports_to_slack "
"and status code %s" % response.status_code)
else:
print("Failed to post report on slack channel "
"and status code %s" % response.status_code)
請幫助解決問題。
我需要在 files.upload API 負載中添加“content”參數和“filename”參數而不是“file”參數,現在文件上傳到松弛通道工作正常。
import requests
def post_reports_to_slack(html_report):
url = "https://slack.com/api/files.upload"
with open(html_report) as fh:
html_data = fh.read()
data = {
"token": bot_user_token,
"channels": "#channel_name",
"content": html_data,
"filename": "report.html",
"filetype": "html",
}
response = requests.post(
url=url, data=data,
headers={"Content-Type": "application/x-www-form-urlencoded"})
if response.status_code == 200:
print("successfully completed post_reports_to_slack "
"and status code %s" % response.status_code)
else:
print("Failed to post report on slack channel "
"and status code %s" % response.status_code)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.