Skip to content

Instantly share code, notes, and snippets.

@ErikKalkoken
Last active June 12, 2023 12:15
Show Gist options
  • Save ErikKalkoken/f3ab28617d5099531ded865d111dbac1 to your computer and use it in GitHub Desktop.
Save ErikKalkoken/f3ab28617d5099531ded865d111dbac1 to your computer and use it in GitHub Desktop.
Fetch a private file from Slack
# 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)
@kiranmah92
Copy link

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 ?

@ErikKalkoken
Copy link
Author

ErikKalkoken commented Aug 27, 2020

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.

@paloha
Copy link

paloha commented Jul 28, 2022

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.

@reverie-ss
Copy link

There's a mistake in this code:
You are missing f in L9
{f'Authorization': f'Bearer {slack_token}'})

@ErikKalkoken
Copy link
Author

@reverie-ss thanks for heads up. the f was actually in the wrong place. Corrected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment