-
-
Save ErikKalkoken/f3ab28617d5099531ded865d111dbac1 to your computer and use it in GitHub Desktop.
# fetching a private Slack file with requests | |
import requests | |
import os | |
slack_token = os.environ['SLACK_TOKEN'] | |
url = 'https://files.slack.com/files-pri/T12345678-F12345678/my_file.jpg' | |
res = requests.get(url, headers={'Authorization': f'Bearer {slack_token}'}) | |
res.raise_for_status() | |
with open('my_file.jpg', 'wb') as f: | |
f.write(res.content) | |
above code is working fine. But the image that i save is corrupted. after fetching image from slack , if i print "res.content" it is textual information and not byte code. Can you identify what is the problem ?
Hey @kiranmah92! If you try to access a file without the right permissions / token then you get a HTML asking you to login. Sounds like that might be your problem here.
If somebody came here from google, you might also be interested in this script: https://github.com/auino/slack-downloader
I have just created a pull request such that it works with the latest Slack API.
There's a mistake in this code:
You are missing f
in L9
{f'Authorization': f'Bearer {slack_token}'})
@reverie-ss thanks for heads up. the f
was actually in the wrong place. Corrected.
@moradamir
Thanks for the feedback. I think that is actually a bug and the script is meant to use
slack_token
. I will update it.