0

I am trying to use python to create an event in google calendars. The error lies with how part way through there seems to be an issue with using .get(). I am not extremely experienced with the google calendar api as this is my first program so I can't narrow it down anymore, so can someone help me with this?

I recreated the code suggested on the video at https://developers.google.com/calendar/create-events and I changed storage.json to credentials.json

The line that triggers the error is this,

store = file.Storage('credentials.json')
creds = store.get()

The file just can't run and spits out a bunch of errors.

These are the errors:

Traceback (most recent call last):
  File "goal_insert.py", line 9, in module>
    creds = store.get()
  File "/Users/name/Library/Python/2.7/lib/python/site-packages/oauth2client/client.py", line 407, in get
    return self.locked_get()
  File "/Users/name/Library/Python/2.7/lib/python/site-packages/oauth2client/file.py", line 54, in locked_get
    credentials = client.Credentials.new_from_json(content)
  File "/Users/name/Library/Python/2.7/lib/python/site-packages/oauth2client/client.py", line 302, in new_from_json
    module_name = data['_module']
KeyError: '_module'
Hursh Jha
  • 13
  • 7
  • Have you checked this related [SO post](https://stackoverflow.com/questions/30966906/google-api-quickstart-py-error-keyerror-module)? You can try the solution replacing `creds = store.get()` with `creds = None` for the meantime. – Jessica Rodriguez Jun 05 '19 at 12:42
  • @jess I commented out the line and the following line and it seems to work. However, this caused the issue of the credentials file being changed each time I ran it. So I ended up reading the initial JSON file and after the program finished it would dump that info back into the JSON file so that it was at the same state it was at the beginning. However, I am not sure if that will work as I expand on this so I was thinking that I might need to fix it. I am looking at the other thread rn to see what I can use from it though. – Hursh Jha Jun 05 '19 at 14:04

2 Answers2

0

I don't know why, but commenting out the

creds = store.get() 

line and the line after made it work, I don't know why to be honest but that fixed it. That seems to change the credentials file, then I could uncomment the lines and it worked again.

Hursh Jha
  • 13
  • 7
0

My suggestion is that you should start with the quickstart here:

https://developers.google.com/calendar/quickstart/python

Also there is an explanation on how to create an event:

https://developers.google.com/calendar/create-events

You should be able to run your code putting together those 2 examples. Here is an implementation example following the quickstart instructions (installing python, pip, google libraries and putting the credentials.json file in the same directory with the script) and adding the create event functionality to the quickstart script:

from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/calendar']


def main():
    """Shows basic usage of the Admin SDK Directory API.
    Prints the emails and names of the first 10 users in the domain.
    """
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server()
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    serviceCalendar = build('calendar', 'v3', credentials=creds)

    #Create event with invitees    
    event = {
        'summary': 'Liron clone wars training',
        'location': 'Barcelona city',
        'description': 'A chance to hear more about Google\'s developer products.',
        'start': {
            'dateTime': '2019-06-08T00:00:00',
            'timeZone': 'America/Los_Angeles',
        },
        'end': {
            'dateTime': '2019-06-08T08:00:00',
            'timeZone': 'America/Los_Angeles',
        },
        'attendees': [{"email": "random1@domain.eu"}, {"email": "random2@domain.eu"}]
    }

    event = serviceCalendar.events().insert(calendarId='primary', body=event).execute()
    print('Event created: %s' % (event.get('htmlLink')))

if __name__ == '__main__':
    main()
Andres Duarte
  • 3,166
  • 1
  • 7
  • 14