|
5 | 5 | It optionally opens a browser window to guide a human user to manually login. |
6 | 6 | After obtaining an auth code, the web server will automatically shut down. |
7 | 7 | """ |
8 | | - |
9 | | -import argparse |
10 | 8 | import webbrowser |
11 | 9 | import logging |
12 | 10 |
|
|
18 | 16 | from urlparse import urlparse, parse_qs |
19 | 17 | from urllib import urlencode |
20 | 18 |
|
21 | | -from .oauth2 import Client |
22 | | - |
23 | 19 |
|
24 | 20 | logger = logging.getLogger(__name__) |
25 | 21 |
|
@@ -95,17 +91,24 @@ def _send_full_response(self, body, is_ok=True): |
95 | 91 | self.wfile.write(body.encode("utf-8")) |
96 | 92 |
|
97 | 93 |
|
| 94 | +# Note: Manually use or test this module by: |
| 95 | +# python -m path.to.this.file -h |
98 | 96 | if __name__ == '__main__': |
| 97 | + import argparse |
| 98 | + from .oauth2 import Client |
99 | 99 | logging.basicConfig(level=logging.INFO) |
100 | 100 | p = parser = argparse.ArgumentParser( |
| 101 | + formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
101 | 102 | description=__doc__ + "The auth code received will be shown at stdout.") |
102 | | - p.add_argument('endpoint', |
103 | | - help="The auth endpoint for your app. For example: " |
104 | | - "https://login.microsoftonline.com/your_tenant/oauth2/authorize") |
| 103 | + p.add_argument( |
| 104 | + '--endpoint', help="The auth endpoint for your app.", |
| 105 | + default="https://login.microsoftonline.com/common/oauth2/v2.0/authorize") |
105 | 106 | p.add_argument('client_id', help="The client_id of your application") |
106 | | - p.add_argument('redirect_port', type=int, help="The port in redirect_uri") |
| 107 | + p.add_argument('--port', type=int, default=8000, help="The port in redirect_uri") |
| 108 | + p.add_argument('--scope', default=None, help="The scope list") |
107 | 109 | args = parser.parse_args() |
108 | | - client = Client(args.client_id, authorization_endpoint=args.endpoint) |
109 | | - auth_uri = client.build_auth_request_uri("code") |
110 | | - print(obtain_auth_code(args.redirect_port, auth_uri)) |
| 110 | + client = Client({"authorization_endpoint": args.endpoint}, args.client_id) |
| 111 | + auth_uri = client.build_auth_request_uri( |
| 112 | + "code", scope=args.scope, redirect_uri="http://localhost:%d" % args.port) |
| 113 | + print(obtain_auth_code(args.port, auth_uri)) |
111 | 114 |
|
0 commit comments