0

I am writing a Python script that interacts with the Spotify API. To authenticate, I generate a url that looks something like this: https://accounts.spotify.com/authorize?response_type=token&client_id=<client_id_here>&redirect_uri=http%3A%2F%2F127.0.0.1%3A8083. Spotify then redirects me to my redirect uri, which is currently localhost (127.0.0.1:8083). Spotify attaches the access token as a fragment to the redirect uri. I need to access the uri fragment from Python.

Currently I am using the following code (the function generate_login_url() generates the accounts.spotify.com url I need)

webbrowser.open(generate_login_url())

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
    sock.bind(('127.0.0.1', 8083))
    sock.listen()
    conn, _ = sock.accept()
    with conn:
        data = conn.recv(1024)
        conn.sendall(data)

When I run this, I can see in the web browser that I have been redirected to the correct url (http://127.0.0.1:8083/#access_token=<access_token_here>&token_type=Bearer&expires_in=3600) but I cannot find a way to access the fragment from python. Calling conn.recv(1024) again causes a timeout, and the information I need is not found anywhere in data, which seems to just hold the user-agent string and the headers.

  • I don't know exactly what's happening but your code obviously isn't capable of running the server side of HTTP. It's hard to test for the rest of us without having a similar capability at spotify. – President James K. Polk Dec 11 '22 at 19:50

0 Answers0