Search code examples
pythondownloadslack-api

Python Slack API download binary file returns html


I can upload files to a slack channel via the python Slack API. I am unable to download the binary file with the url indicated in message['files'][0]['url_private_download'].

Rather, I get 40KB of HTML when using requests.get, wget -mO <file-name> <url>, and curl <url> --output <file-name>.

Oddly, I can enter the same url in the chrome browser and the binary file is downloaded.

It appears as though something is going on behind the scenes. And I'm not sure how best to approach this.

Suggestions?


Solution

  • When an HTTP client retrieves files, it needs to present its credentials in a similar way to the API. If your token has the right permissions, the HTTP request will respond with the file contents.

    In curl requests you would typically present your bot token as a bearer token using an argument like -H "Authorization: Bearer xoxb-xxxxxxxxx-xxxx". Header-based authentication is required specifically in this context.

    The request works in your browser due to the session and cookie carrying the authorization information about the user and the workspace you're interacting with.