-
Notifications
You must be signed in to change notification settings - Fork 0
Google credentials
Andrew Ang edited this page Aug 2, 2018
·
9 revisions
Examples for generating google credentials for use with gdocrevisions:
With service account authentication:
- Google Doc must be shared with the service account email
- No additional authentication step needed on code execution
https://console.cloud.google.com/apis/credentials -> Create credentials -> Service account key
Select options:
- service account: App engine default service account (or custom service account if you have created one)
- Key type = JSON
Creates and downloads a credential file with name format project-xxx.json.
- Open the credential file and identify the client email (value of
client_email; will typically end with...@appspot.gserviceaccount.comor...@iam.gserviceaccount.com). - Share the Google Doc with the client email.
Requirements:
- google-auth (pip install google-auth)
from google.oauth2 import service_account
CREDENTIAL_FILE = 'project-xxx.json'
SCOPE = ['https://www.googleapis.com/auth/drive.readonly']
credentials = service_account.Credentials.from_service_account_file(CREDENTIAL_FILE, scopes=SCOPE)
gdoc = GoogleDoc(...)
Now you can use the credentials object with gdocrevisions:
from gdocrevisions import GoogleDoc
GoogleDoc(FILE_ID, credentials)
- Authentication flow occurs on code execution - choose the google account that has access to the document.
https://console.cloud.google.com/apis/credentials -> Create credentials -> Oauthclient ID
Select options:
- Application type = Other
- Name = whatever you want
Creates and downloads credential file with name format
client_secret_xxx-xxx.apps.googleusercontent.com.json
Requirements:
- google-auth-oauthlib (pip install google-auth-oauthlib)
from google_auth_oauthlib.flow import InstalledAppFlow
SCOPE = ['https://www.googleapis.com/auth/drive.readonly']
CREDENTIAL_FILE = 'client_secret_xxx-xxx.apps.googleusercontent.com.json'
flow = InstalledAppFlow.from_client_secrets_file(
CREDENTIAL_FILE,
scopes=SCOPE)
credentials = flow.run_local_server(port=5555)
Now you can use the credentials object with gdocrevisions:
from gdocrevisions import GoogleDoc
GoogleDoc(FILE_ID, credentials)