Last active
April 19, 2018 18:53
-
-
Save kunanit/7538f6f584d1813601933401851daa70 to your computer and use it in GitHub Desktop.
Generate google credentials
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 google.oauth2 import service_account | |
from google_auth_oauthlib.flow import InstalledAppFlow | |
SCOPE = ['https://www.googleapis.com/auth/drive'] # example scope | |
def get_google_credentials_oauth2(client_secrets_file, port=5555): | |
""" | |
get credentials via oauth flow | |
opens up a browser for user to sign in | |
requires client secrets file (select application type = 'other' when creating client in google console) | |
Sets default web server port to 5555, since 8080 not usually available locally | |
""" | |
flow = InstalledAppFlow.from_client_secrets_file( | |
client_secrets_file, | |
scopes=SCOPE) | |
return flow.run_local_server(port=port) | |
def get_google_credentials_service_account(credential_file=None): | |
""" | |
Get credentials based on service account credential file. | |
Will detect file path specified as GOOGLE_APPLICATION_CREDENTIALS environment variable. | |
""" | |
if not credential_file: | |
credential_file = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS') | |
if not credential_file: | |
raise Exception("Service account file not found (Is GOOGLE_APPLICATION_CREDENTIALS env var set?)") | |
return service_account.Credentials.from_service_account_file(credential_file, scopes=SCOPE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment