重定向页面显示在主机上

时间:2015-09-17 22:47:29

标签: python redirect routing bottle

所以我正在尝试构建一个小应用程序来连接Box API,我正在为我的计算机上的用户托管它。过程如下:用户打开/index.html,填写表单,单击提交,重定向到Box以登录并授予访问权限,重定向路由将处理返回的URL并执行用户的请求。现在我已经让它在我的计算机上工作得很好,但是当我的用户尝试它时,他们没有看到重定向网址,但是我的计算机会弹出一个要求我授予对Box的访问权限。我是Bottle和Python的新手,所以我有点迷失在这里......

@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)

1 个答案:

答案 0 :(得分:0)

所以朋友提示我的代码有错误:

webbrowser.open(auth_url)

webbrowser将在我的本地机器上打开浏览器,所以我应该使用重定向。所以我把这个修好了:

bottle.redirect(auth_url)

现在一切都很好