我正在编写一个脚本,以使用slack api和Python中的请求库从Slack下载文件。每当我下载文件时,它们的大小都相同(80kb),并且它们都已损坏。
这是我的代码:
def download_file(url, out):
try:
os.stat(out)
except:
os.mkdir(out)
local_filename = out + '\\' + url.split('/')[-1]
print('outputting to file: %s' % local_filename)
response = requests.get(url, stream=True)
with open(local_filename, 'wb') as f:
response.raw.decode_content = True
shutil.copyfileobj(response.raw,f)
return local_filename
我已经尝试过在SO中张贴各种不同的方法来下载文件,但没有成功。我还检查了我从Slack API获取的URL,它们是正确的,因为我可以将其粘贴到浏览器中并下载文件。
任何帮助,将不胜感激!
我发现了我的问题。由于我使用的是从Slack API文件对象下载图像的私有url,因此除了带有令牌的基本请求外,我还需要包含标头。为此,请使用请求API:
response = request.get(url,stream = True, headers={'Authorization':'Bearer ' + my_token})
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句