Last active
October 28, 2022 17:57
-
-
Save jay0lee/4143fbf48146ddecf7c8301a69b6f4cb to your computer and use it in GitHub Desktop.
App Engine Workspace Service Account Example
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
runtime: python310 | |
handlers: | |
- url: /.* | |
script: auto |
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
from flask import Flask, request | |
import google.auth | |
from googleapiclient.discovery import build | |
from googleapiclient.errors import HttpError | |
app = Flask(__name__) | |
@app.route('/') | |
def root(): | |
userKey = request.args.get('email') | |
if not userKey: | |
return 'you need to add an email parameter like ' \ | |
'<a href="/?email=jdoe@acme.com">' \ | |
'/?email=jdoe@acme.com</a>' | |
scopes = [ | |
'email', | |
'https://www.googleapis.com/auth/admin.directory.user.readonly', | |
] | |
creds, _ = google.auth.default(scopes=scopes) | |
svc = build('admin', 'directory_v1', credentials=creds) | |
try: | |
result = svc.users().get( | |
userKey=userKey, | |
fields='name', | |
).execute() | |
except HttpError as e: | |
return e.reason | |
return f'Hello {result["name"]["givenName"]}' | |
if __name__ == '__main__': | |
app.run(host='127.0.0.1', | |
port=8080, | |
debug=True) |
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
Flask==2.1.0 | |
google-auth | |
google-api-python-client |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment