Reputation: 1132
In my project i have developed a python script for check and create automatically an appointment on my google calendar. First i create from google dev console OAUTH json file and then authenticate and run my script:
SCOPES = 'https://www.googleapis.com/auth/calendar'
CLIENT_SECRET_FILE = 'client_id.json'
APPLICATION_NAME = 'Google Calendar API Python Quickstart'
def get_credentials():
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'calendar-python-quickstart.json')
store = Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('calendar', 'v3', http=http)
when i run it from my local machine all done, but when i upload the scripts and all dependencies using an AWS lambda function when i test it the server responde:
{ "errorMessage": "module initialization error" }
START RequestId: 2244eefe-e3dd-11e7-a0ce-93ed40a4fa13 Version: $LATEST module initialization error: [Errno 30] Read-only file system: '/home/sbx_user1060'
I think this is because the authentication method store and retrive the json credential file on local machine, but how can i authenticate and run code from server-server instead client-server?
thanks in advance
Upvotes: 0
Views: 479
Reputation: 846
In the credentials part of your code, change it to get the default credentials from the machine, like this:
#import GoogleCredentials
from oauth2client.client import GoogleCredentials
credentials = GoogleCredentials.get_application_default()
service = discovery.build('calendar', 'v3', credentials=credentials)
On your lambda configuration set GOOGLE_APPLICATION_CREDENTIALS as environment variable key and your credentials json file name as value.
It works on all my lambdas that use google api.
Upvotes: 1