snow
snow

Reputation: 25

redirect flask with token

I'm new in flask, and i got confused when redirecting page.

so here is the problem, I have to redirect to 3rd party for my login page example:

@app.route('/hk/login/3rdparty', methods=['GET'])
def login_3rdparty():
  if request.method == 'GET':
    app_id = 'xxxxx'
    secret = 'xxxxx'
    data = 3rdparty.connect(app_id, secret)
  return data

and when i direct to the web it return like this:

{"url":"http://demo3.3rdparty.com/partner/code/hk?token=sometoke-sometoken-sometoken"}

how do i redirect from the return json? so after i direct to /hk/login/3rdparty it will redirect to the web with the token on the back of its url

sorry for my bad english

Upvotes: 2

Views: 953

Answers (1)

coralvanda
coralvanda

Reputation: 6606

If the data variable in your question contains the json object you need, then you can use the redirect() function to send your user to the address it contains. Your answer combines both our comments, but I want to add this to make sure others can see the necessary import statements as well.

import json
from flask import redirect

@app.route('/hk/login/3rdparty')
def login_3rdparty():
    app_id = 'xxxxx'
    secret = 'xxxxx'
    raw_data = 3rdparty.connect(app_id, secret)
    data = json.loads(raw_data)
    return redirect (data['url'])

I don't think you even need to specify GET as the method if that's the only method this function uses. Keep up the good work, SNOW.

Upvotes: 1

Related Questions