-1

Spotify's API returns authorisation code generated after user's verification in the following format:

<customer_callback_URL>?code=1234567

To capture the code in my Flask app, I've created a dynamic URL:

@app.route("/<auth_code>")
def callback_code(auth_code):
    return auth_code

Unfortunately, after receiving correct callback, the URL in the browser's address bar looks like this: http://127.0.0.1:5000/?code=1234567

Unfortunately, the dynamic URL ending ?code=1234567 is not generated and Flask server generates 404 error.

This is due to parameters (preceded by "?") are present in the callback URI. That makes the callback_code function unable to return the ?code=1234567 function of the URI.

How can I capture either the callback code string or callback's parameters, so that I can use the authorisation code in the other parts of my Flask app?

Grzesiek
  • 43
  • 5

1 Answers1

0

That code is part of the query string. What you want is:

@app.route("/")
def callback_code():
    return request.args.get('code')

Note that you are sending this to the root url, which is probably not what you want. But it is OK if you don't mind testing every call for the code param

@app.route("/")
def index():
    if request.args.get('code'):
        # test for match and authorize
    else:
        # it's just a normal call to your domain's root url
    
GAEfan
  • 11,244
  • 2
  • 17
  • 33