from oauth2client.service_account import ServiceAccountCredentials # load the service account key from JSON file creds = ServiceAccountCredentials.from_json_keyfile_name('service-account-key.json', ['https://www.googleapis.com/auth/calendar'])
from oauth2client.service_account import ServiceAccountCredentials # load the service account key from JSON string json_str = ('{"private_key_id": "...", "private_key": "...", "client_email": "...", "client_id": "..."}') creds = ServiceAccountCredentials.from_json_keyfile_dict(json.loads(json_str), ['https://www.googleapis.com/auth/calendar'])
from googleapiclient.discovery import build from oauth2client.service_account import ServiceAccountCredentials # create credentials object creds = ServiceAccountCredentials.from_json_keyfile_name('service-account-key.json', ['https://www.googleapis.com/auth/calendar']) # build a client object service = build('calendar', 'v3', credentials=creds) # make an API call events = service.events().list(calendarId='primary', maxResults=10).execute()This code uses the `ServiceAccountCredentials` object to authenticate a client for the Google Calendar API. It then makes an API call to list the next 10 events on the user's primary calendar. Overall, the `oauth2client.service_account` library provides a convenient way to authenticate with Google APIs using a service account, making it easier to interact with these APIs programmatically.