且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

蟒蛇 - 获取URL的浏览器重定向到

更新时间:2022-05-09 00:06:10

试着启动你自己的小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.