How to make a url redirect in flask outside of @app.route

Asked

Viewed 11 times

-1

I’m trying to consume a free market api using Python along with flask, to be able to use this API, I need to generate a token and to generate a token, I need a client authorization code, ie, will open a tab to put the user and password for my application get the code, I’m doing it like this:

@app.route('/code')
def code():
    print('Gerando um novo code')
    return redirect('https://auth.mercadolivre.com.br/authorization?response_type=code&client_id=*********&redirect_uri=https://test/gera_token')

@app.route('/gera_token')
def gera_token():
  
        code = request.args.get('code')
        if code:
            resp = token_principal(code)
            return resp
        else:
            return redirect('O code não foi encontrado')

En route '/code' I will direct the user to authorize my application and en route '/gera_token' I receive the code and call the function that is in another file to generate the token

def token_principal(code):
    

    url = 'https://api.mercadolibre.com/oauth/token'

    header = {
        'accept'       : 'application/json',
        'content-type' : 'application/x-www-form-urlencoded'
    }

    dados = {
        'grant_type' : 'authorization_code' ,
        'client_id' : '********' ,
        'client_secret' : '*************' ,
        'code' : code, 
        'redirect_uri' : 'https://test/gera_token'
    }

    requisicao = requests.post(url,
                            headers = header,
                            data = dados)

    
    return requisicao.text 

This code is working, but I would like to do this whole process in a single function, ie within the function token_principal(), be able to redirect the user to ML pay the code and generate the token.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.