I'm using a Slack Bolt Python SDK to collect files uploaded by user. When a user uploads a file to the slack bot app, the bot will recognize the event and the reveived body is sth like:
{'token': 'xxxxxxxxxxxx',
'team_id': 'Ttttttttt',
'enterprise_id': 'Eeeeee',
'api_app_id': 'Aaaaaaaa',
'event': {'type': 'message',
'text': '',
'files': [{'id': 'Fffffff',
......
'mode': 'snippet',
'is_external': False,
'external_type': '',
'is_public': False,
'public_url_shared': False,
'display_as_bot': False, 'username': '',
'url_private': 'https://url/file.xlsx',
'url_private_download': 'https://url2/file.xlsx',
'permalink': 'https://url3/file.xlsx',
'permalink_public': 'https://url3',
......
Given the returned value, how can I obtain the file and save it? I have tried sth like
file_path = body['event']['files'][0]['url_private_download']
urllib.request.urlretrieve (file_path, "./temple.xlsx")
But the file saved is not in xlsx format but in html format. Can anyone tell me how to save the file correctly? Thanks in advance.
You should provide an authorization header while making the request to download the file:
Authorization: Bearer A_VALID_TOKEN
A_VALID_TOKEN is a OAuth token, that has files.read
scope.
You can read more here: https://api.slack.com/types/file#auth
It looks like the html file you get is just a default response from Slack when there is no auth token provided.