Skip to content

Commit e6be9ec

Browse files
committed
authcode.py CLI has long been broken. Now fixed.
1 parent 1846f91 commit e6be9ec

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

oauth2cli/authcode.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
It optionally opens a browser window to guide a human user to manually login.
66
After obtaining an auth code, the web server will automatically shut down.
77
"""
8-
9-
import argparse
108
import webbrowser
119
import logging
1210

@@ -18,8 +16,6 @@
1816
from urlparse import urlparse, parse_qs
1917
from urllib import urlencode
2018

21-
from .oauth2 import Client
22-
2319

2420
logger = logging.getLogger(__name__)
2521

@@ -95,17 +91,24 @@ def _send_full_response(self, body, is_ok=True):
9591
self.wfile.write(body.encode("utf-8"))
9692

9793

94+
# Note: Manually use or test this module by:
95+
# python -m path.to.this.file -h
9896
if __name__ == '__main__':
97+
import argparse
98+
from .oauth2 import Client
9999
logging.basicConfig(level=logging.INFO)
100100
p = parser = argparse.ArgumentParser(
101+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
101102
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")
105106
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")
107109
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))
111114

0 commit comments

Comments
 (0)