-1

I'm trying to collect this specific data but it gives the error:

print (resp["authorization"])
KeyError: 'authorization'

Code: https://i.stack.imgur.com/jnxbi.png

What I would like to collect:

https://i.stack.imgur.com/uPwum.png

What should I do to be able to collect this data?

1 Answers1

0

The authorization parameter seems to be in the request header. But, you're trying to find it in the response header where it is unavailable. So when you are trying to access the data using the authorization parameter from the dictionary it returns KeyError

To see the headers you've sent, try the following:

from requests import get

res = get("https://discord.com/api/v8/applications/detectable")

print(res.request.headers)

And please try to always post the code and error message in text, not as screenshots like these.

dreygur
  • 300
  • 3
  • 12
  • I just made a change to the answer. Please have a look. – dreygur Nov 15 '20 at 19:22
  • print return only: ```{'User-Agent': 'python-requests/2.24.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}``` – AndersonPGS Nov 15 '20 at 19:31
  • This is the header your script requesting with. – dreygur Nov 15 '20 at 19:33
  • I would like to collect the ```authorization```, is it possible? – AndersonPGS Nov 15 '20 at 19:34
  • The `authorization` header isn't from the remote host, you have to provide it. So I think just copy the token from the browser and send it with your request. But there must be a way where you can get the token by requesting the remote. As I'm not familiar with their API, I can't say how. – dreygur Nov 15 '20 at 19:37
  • any way to collect this token using python would be very welcome, I am using selenium to login to the page, I even tried to use it to collect this data with devtools, however I believe it is not possible, do you know any way to collect this data? even if it's not for requests – AndersonPGS Nov 15 '20 at 19:42
  • From selenium the answer is no. But you can use seleniumwire which is a selenium extension. Or can use JS to retrive the headers. Like this: ```headers = driver.execute_script("var req = new XMLHttpRequest(); req.open('GET', document.location, false); req.send(null); return req.getAllResponseHeaders()"); headers = headers.splitlines()``` There is an [answer](https://stackoverflow.com/questions/62262261/how-to-get-request-headers-in-selenium) – dreygur Nov 15 '20 at 19:48
  • Thanks for the tip, returned a lot of data but nothing from the ```authorization``` – AndersonPGS Nov 15 '20 at 20:04
  • You have to find it by requesting your specified URL. There it should available. – dreygur Nov 15 '20 at 20:07
  • where do i specify this url? – AndersonPGS Nov 15 '20 at 21:53