11import os
2+ import json
23from apiclient .discovery import build
34from oauth2client .service_account import ServiceAccountCredentials
45
56SCOPES = ['https://www.googleapis.com/auth/calendar.readonly' ]
67
7- def get_service (api_name , api_version , scopes , key_file_location ):
8+ def get_service (api_name , api_version , scopes , key_file_contents ):
89 """Get a service that communicates to a Google API.
910
1011 Args:
1112 api_name: The name of the api to connect to.
1213 api_version: The api version to connect to.
1314 scopes: A list auth scopes to authorize for the application.
14- key_file_location : The path to a valid service account JSON key file .
15+ key_file_contents : The contents of json keyfile .
1516
1617 Returns:
1718 A service that is connected to the specified API.
1819 """
1920
20- credentials = ServiceAccountCredentials .from_json_keyfile_name (key_file_location , scopes = scopes )
21+ key_file_json = json .loads (key_file_contents )
22+
23+ credentials = ServiceAccountCredentials .from_json_keyfile_dict (key_file_json , scopes = scopes )
2124
2225 # Build the service object.
2326 service = build (api_name , api_version , credentials = credentials )
2427
2528 return service
2629
2730# Authenticate and construct service.
28- calendar_service = get_service (api_name = 'calendar' , api_version = 'v3' , scopes = SCOPES , key_file_location = os .path .join (os .getcwd (), "secrets" , "client_secrets.json" ))
31+ def get_calendar_service (key_file_contents ):
32+ return get_service (api_name = 'calendar' , api_version = 'v3' , scopes = SCOPES , key_file_contents = key_file_contents )
0 commit comments