Python request.get() is not following redirects

52 Views Asked by At

I am trying to use the spotify api which uses OAuth. Once you have been authorized, accessing the authorization url redirects you to your specified redirect_uri with a code in the URL parameters. When I access the authorization url using

import webbrowser
webbrowser.open(authorization_url) 

it redirects me to my specified redirect uri and I can see the code in the URL on my browser. However, when I read the URL from the requests.get(), as in:

import requests
r = requests.get(authorization_url, allow_redirects = True)
print(r.url)

Then r.url holds the original authorization_url, and not the redirect uri with the code.

I tried investigating further by looking into the request history with:

import requests
r = requests.get(authorization_url, allow_redirects = True)
print(r.history)
for resp in r.history:
    print(r.url)

And r.history is a list with only one item [<Response [303]>], which is strange because I've read that 303 means that there was in fact a redirection. But printing r.url from that item in the for loop only gives me the original authorization_url again.

0

There are 0 best solutions below