-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Google oauth handler using an external Python script #345
Open
gaganpreet
wants to merge
6
commits into
Zren:master
Choose a base branch
from
gaganpreet:google-calendar-fix
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
87d9755
Google oauth handler using an external Python script
gaganpreet 7a7cc2e
Extract exchange code logic into function
gaganpreet 0e0e3ea
Use tabs
gaganpreet 22844ef
Remove debug output
gaganpreet aadf4b4
handle scenario when code param is missing
gaganpreet bdad0b7
Use encodeURIComponent for param
gaganpreet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
"""Script to handle oauth redirects from Google""" | ||
|
||
import json | ||
import urllib.parse | ||
import urllib.request | ||
import urllib.error | ||
import argparse | ||
from http.server import BaseHTTPRequestHandler, HTTPServer | ||
from urllib.parse import urlparse, parse_qs | ||
|
||
client_id = client_secret = listen_port = None | ||
|
||
|
||
def exchange_code_for_token(code): | ||
# Exchange code for token from https://oauth2.googleapis.com/token | ||
# using the following POST request: | ||
token_params = { | ||
"code": code, | ||
"client_id": client_id, | ||
"client_secret": client_secret, | ||
"redirect_uri": "http://127.0.0.1:{}/".format(listen_port), | ||
"grant_type": "authorization_code", | ||
} | ||
data = urllib.parse.urlencode(token_params).encode("utf-8") | ||
req = urllib.request.Request("https://oauth2.googleapis.com/token", data) | ||
response = urllib.request.urlopen(req) | ||
token_data = json.loads(response.read().decode("utf-8")) | ||
return token_data | ||
|
||
|
||
class OAuthRedirectHandler(BaseHTTPRequestHandler): | ||
def do_GET(self): | ||
query = urlparse(self.path).query | ||
params = parse_qs(query) | ||
# handle OAuth redirect here | ||
if "code" in params: | ||
code = params["code"][0] | ||
try: | ||
token_data = exchange_code_for_token(code) | ||
except urllib.error.HTTPError as e: | ||
print(e.read().decode("utf-8")) | ||
self.wfile.write(b"Handling redirect failed.") | ||
raise SystemExit(1) | ||
print(json.dumps(token_data, sort_keys=True)) | ||
|
||
self.send_response(200) | ||
self.send_header("Content-type", "text/html") | ||
self.end_headers() | ||
self.wfile.write( | ||
b"OAuth redirect handled successfully. You can close this tab now." | ||
) | ||
raise SystemExit(0) | ||
self.wfile.write(b"Missing code parameter in redirect.") | ||
raise SystemExit(1) | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--client_id", required=True) | ||
parser.add_argument("--client_secret", required=True) | ||
parser.add_argument("--listen_port", required=True, type=int) | ||
args = parser.parse_args() | ||
client_id = args.client_id | ||
client_secret = args.client_secret | ||
listen_port = args.listen_port | ||
|
||
server_address = ("", listen_port) | ||
httpd = HTTPServer(server_address, OAuthRedirectHandler) | ||
httpd.serve_forever() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the path of this file? Or is it stdout? or stderr? Is there a reason for using write instead of
print()
(no newline)?https://docs.python.org/3/library/http.server.html#http.server.BaseHTTPRequestHandler.wfile
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.wfile
is to write the response back to the client, in this case the browser (to guide the user what to do). For example, after a successful redirect, the user will see this in their browser:The print output is for communication with the QML script (using the
executable.exec
helper).