1

I used to have a working python function to fetch files from a specific Slack channel, but that stopped working a few months ago.

I tested the same request to the slack API (files.list) using Postman which does give me an array with a number of files.

The following code used to work but no longer does:

import requests
import json
apiBase = "https://slack.com/api/"
accesToken = "Bearer xoxb-<secret>"
requestData = { "channel": "<obscured>" }
r = requests.post(apiBase + "files.list", headers={'Authorization': accesToken, 'Content-Type': 'application/json; charset=utf-8'}, data=requestData)
try:
 response = json.loads(r.text)
except:
 print("Read error")
 isError = True
if(not 'files' in response):
 if('error' in response):
 print(response['error'])
 if('warning' in response):
 print(response['warning'])
 isError = True
files = response['files']
files.sort(key=lambda x:x['timestamp'])
count = len(files)
print(str(r))
print(str(r.request.body))
print(str(r.request.headers['Content-Type']))
print(str(r.text))

The result is:

<Response [200]>
channel=<secret>
application/json; charset=utf-8
{"ok":true,"files":[],"paging":{"count":100,"total":0,"page":1,"pages":0}}
Process finished with exit code 0

Postman also returns a 200 OK, but the array contains 3 files for this channel. So why is Python not getting the 3 files...?

I know that an app needs to be given access to the channel in Slack which is the case here. (The channel and credentials are identical in both scenario's (Python and Postman).

Please advise me ...

asked May 6 at 8:26
4
  • The request you're sending here and the one you're sending via Postman are different, that's why the responses are different, too. Note: From the info you provide, it's impossible to actually tell what is wrong or different. Commented May 6 at 9:55
  • Postman has function to generate code in Python (and other languages). Sometimes documentation may have example in Python. Or it may have example for curl and there are pages like CurlConverter which can convert curl to Python (and other languages) (but sometimes it can make small mistake in code) Commented May 6 at 10:09
  • data= sends dictionary as FORM, not JSON. It may need json= to send it as JSON (eventually you may convert requestData to string with JSON data - using json.dumps() - and use it with data=) Commented May 6 at 10:13
  • This is the working solution: r = requests.post(apiBase + "files.list", headers={'Authorization': accesToken, 'Content-Type': 'application/json'}, params=requestData) Commented May 6 at 11:02

1 Answer 1

1

I think it has something to do with the content-type you send with the requests.post
Have you tried using

json=requestData

instead of

data=requestData

Even though the content-type is correctly set in your headers the requests.post might still send "data" as a dictionary, this is maybe why the slack api is ignoring your data.

Update:

The solution was to put the requestData into the url as a parameter, this can be elegantly done using the "params" argument of the requests.post() function like so:

requestData = { "channel": channel_id } 
requests.post(params=requestData)

This is the way the Slack api expects the data.

answered May 6 at 10:01
Sign up to request clarification or add additional context in comments.

4 Comments

I tested json=requestData and that yields the same output. But you might be unto something here.
You are right, the url is not extended with the parameters. When I change the code to this: r = requests.post(apiBase + "files.list?channel=<secret>", header.... I DO get the list of files. I still wanna checkout what is the correct way to pass in the parameters, but this gives me a working workaround...
Then I think it would be best to use the params argument of the post() function requestData = { "channel": channel_id } requests.post(params=requestData)
Thanks for the feedback, I updated the answer for potential future readers.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.