0

My goal is to connect to the Spotify API using pure Python and I have been able to figure out how to obtain the authorization token given the authorization code but I am unable to get the authorization code itself.

Note: I have not provided the client_id and client_secret for obvious reasons and you can assume that all libraries have been imported.

Once the web browser opens, the authorization code ("code") is displayed as a query parameter in the URL but I am unsure how to go about saving it to a variable. I can't just copy and paste it to a variable as the authorization code constantly changes.

My question is how exactly I would go about retrieving the code query paramter and save it to a variable?

Here is what I have tried so far:

    # credentials
    client_id = "xxx..."
    client_secret = "xxx..."

    # urls
    redirect_uri = "http://localhost:7777/callback"
    auth_url = "https://accounts.spotify.com/authorize?"
    token_url = "https://accounts.spotify.com/api/token"

    # data scopes
    scopes = "user-read-private user-read-email"

    # obtains authorization code
    payload = {
        "client_id": client_id,
        "response_type": "code",
        "redirect_uri": redirect_uri,
        "scope": scopes
    }
    webbrowser.open(auth_url + urlencode(payload))

    code = # NOT SURE HOW TO RETRIEVE CODE

    # obtains authorization token
    encoded_creds = base64.b64encode(client_id.encode() + b":" + client_secret.encode()).decode('utf-8')
    token_headers = {
        "Authorization": "Basic " + encoded_creds,
        "Content-Type": "application/x-www-form-urlencoded"
    }
    token_data = {
        "grant_type": "authorization_code",
        "code": code,
        "redirect_uri": redirect_uri
    }
    r = req.post(token_url, data=token_data, headers=token_headers)
4
  • What is your question? I don't see a question in your posting. It seems that you need to duplicate in code whatever requests the browser is making to the remote Spotify site that end up with the authorization token showing up in the address bar of the browser. Are you asking what specific requests to make to get that to happen, or is your question more about the nuts and bolts of making requests and querying the resulting responses? Are you using a documented API or are you attempting to reverse engineer what the browser is doing? Commented Dec 2, 2022 at 21:29
  • My question is how exactly I can retrieve the "code" query parameter from the URL and save it to a variable so I can use it to get the access token. Basically, I have a token_data["code"] with no value at the moment and am unsure how to retrieve that value. Commented Dec 2, 2022 at 21:33
  • 1
    Are you getting back a response when you query the auth_url? If so, is the auth code you want in the response somewhere? If it is, then it shouldn't be hard to extract it so that you can use it in your second request. If it's not there, what IS there? Are you asking what additional requests you need to make to get the auth code? Commented Dec 2, 2022 at 21:35
  • Yes I am asking what additional requests (or in general what) I would have to do in order to get the authentication code. For example, the url looks like this: "localhost:7777/callback?code=xxx...". How would I get that code value? Commented Dec 2, 2022 at 21:40

1 Answer 1

1

You'll need to extract the code from your callback URL. If authentication is successful, Spotify will make a request to your redirect_uri with the code in the query (e.g http://localhost:7777/callback?code=...).

The easiest way to do that is probably spin up a Flask server (or equivalent) with a GET callback endpoint and grab the code there. Then you can exchange it for the authorization token in the same endpoint if you'd like. This example may be helpful: https://github.com/spotify/web-api-auth-examples/blob/master/authorization_code/app.js#L60

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I knew that it was possible to do by setting up a Flask server but I was trying to avoid that.

Your Answer

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.