-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample_auth_code_simple.py
More file actions
59 lines (48 loc) · 1.9 KB
/
example_auth_code_simple.py
File metadata and controls
59 lines (48 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from http.server import BaseHTTPRequestHandler, HTTPServer
import urllib.parse as urlparse
import webbrowser
from ecnuopenapi import auth
from ecnuopenapi import oauth_init
# 初始化OAuth客户端配置
config = oauth_init.OAuth2Config(
client_id='client_id',
client_secret='client_secret',
redirect_url='http://localhost:8080/user',
)
oauth_init.initOAuth2AuthorizationCode(config)
class SimpleServer(BaseHTTPRequestHandler):
def do_GET(self):
if self.path.startswith('/login'):
state = auth.generateState()
authorization_url = auth.getAuthorizationEndpoint(state)
self.send_response(302)
self.send_header('Location', authorization_url)
self.end_headers()
elif self.path.startswith('/user'):
parsed_path = urlparse.urlparse(self.path)
query = urlparse.parse_qs(parsed_path.query)
code = query.get('code', [None])[0]
state = query.get('state', [None])[0]
token = auth.getToken(code, state)
if not token:
self._send_text('Failed to fetch token', 400)
return
user_info = auth.getUserInfo(token)
if not user_info:
self._send_text('Failed to fetch user info', 400)
return
self._send_text(f'User Info: {user_info}', 200)
else:
self._send_text('Not Found', 404)
def _send_text(self, text, status_code=200):
self.send_response(status_code)
self.send_header('Content-type', 'text/plain; charset=utf-8')
self.end_headers()
self.wfile.write(text.encode())
def run(server_class=HTTPServer, handler_class=SimpleServer, port=8080):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print(f'Starting httpd on port {port}...')
httpd.serve_forever()
if __name__ == "__main__":
run()