蟒蛇 - 获取URL的浏览器重定向到 [英] Python - Getting url a browser was redirected to

查看:272
本文介绍了蟒蛇 - 获取URL的浏览器重定向到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想验证与API的应用程序。结果
具体方法如下:

I am trying to authenticate an application with the API.
Here's How:


  1. 我打开使用 webbrowser.open 的URL。

  2. 的用户进行身份验证的应用,并重定向到另一个网址,这是结果
    https://stackexchange.com/oauth/login_success 带参数带这个URL codeD。结果
     样本重定向网址是:结果
    ... / login_success#=的access_token XYZ和放大器;过期= 00000

  1. I am opening a URL using webbrowser.open.
  2. The user authenticates the application, and is redirected to another URL, which is
    https://stackexchange.com/oauth/login_successwith arguments encoded with this URL.
    A sample redirect url is:
    .../login_success#access_token=xyz&expires=00000

我目前的code:

auth_url = 'https://stackexchange.com/oauth/dialog'
def authenticate():
    scope = "write_access,private_info,read_inbox"
    url = make_url(auth_url,client_id=132,
                   scope=scope,response_type='code',
                   redirect_uri='https://stackexchange.com/oauth/login_success')
    webbrowser.open(url)

我怎样才能重定向URL的用户验证自己后(用户被带到URL)???

How can I get the redirect URL (the URL the user is taken to) after the user authenticates themselves???

推荐答案

试着启动你自己的小HTTP服务器只是一个请求,让API重定向到它,并等待重定向请求出现。这不是一个完整的例子,只是基本的概念:

Try to fire up your own little HTTP server for just one request, let the API redirect to it and wait for the redirected request to appear. This is not a complete example, just the basic concept:

import BaseHTTPServer
auth_url = 'https://stackexchange.com/oauth/dialog'

# replace with your own http handlers
def wait_for_request(server_class=BaseHTTPServer.HTTPServer,
                     handler_class=BaseHTTPServer.BaseHTTPRequestHandler):
    server_address = ('', 8000)
    httpd = server_class(server_address, handler_class)
    return httpd.handle_request()

def authenticate():
    scope = "write_access,private_info,read_inbox"
    url = make_url(auth_url,client_id=132,
                   scope=scope,response_type='code',
                   redirect_uri='http://localhost:8000/login_success')
    webbrowser.open(url)
    wait_for_request()

您可能需要使用虽然HTTPS。从长远来看,你可能会与现有的OAuth实现更好。

You probably need to use HTTPS though. In the long run, you might be better off with an existing OAuth implementation.

这篇关于蟒蛇 - 获取URL的浏览器重定向到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆