Search code examples
pythonhttp-redirectroutesbottle

Redirect page appear on host instead


So I'm trying to build a small app to connect to Box API and I am hosting it for my users on my computer. The process is this: user opens /index.html, fill in the form, click submit, gets redirect to Box to login and grant access, the redirect route will handle the returned url and perform the user's request. Now I've got it working perfectly fine on my computer, but when my user try it, they do not see the redirect url but my computer will have a pop up asking me to grant access to Box instead. I am new to Bottle and Python so I'm kinda lost here...

@get('/')
def input_info():
    return template('index', message = 0)

@post('/')
def process_info():
    oauth = OAuth2(
        client_id=CLIENT_ID,
        client_secret=CLIENT_SECRET,
    )
    auth_url, csrf_token = oauth.get_authorization_url('https://myhostname:8090/redirect')
    webbrowser.open(auth_url)

@route('/redirect')
def redirecting():

    print("entering redirect")

    try:
        # Get tokens
        oauth = OAuth2(
            client_id=CLIENT_ID,
            client_secret=CLIENT_SECRET,
        )
        parsed = urlparse(request.url)
        code = parse_qs(parsed.query)['code']
        access_token, refresh_token = oauth.authenticate(code)
    except:
        return template('index', message = 8)

    # Setup client
    client = Client(oauth)

Solution

  • So a friend tip that my code has an error at:

    webbrowser.open(auth_url)
    

    That the webbrowser will open up a browser at my machine locally so I should have use redirect instead. So I fixed this one to:

    bottle.redirect(auth_url)
    

    And now all is well