Skip to content

Google credentials

Andrew Ang edited this page Aug 2, 2018 · 9 revisions

Examples for generating google credentials for use with gdocrevisions:

Service account

With service account authentication:

  • Google Doc must be shared with the service account email
  • No additional authentication step needed on code execution

Creating credential in google cloud console

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.

Identify the client email and share google doc with the service account

  • Open the credential file and identify the client email (value of client_email; will typically end with ...@appspot.gserviceaccount.com or ...@iam.gserviceaccount.com).
  • Share the Google Doc with the client email.

Creating credential object in python

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)

oauth2

  • Authentication flow occurs on code execution - choose the google account that has access to the document.

Creating credential in console

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

Creating credential object in python

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)

Other Resources