Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Google Calendar & Python
#1
Hello Guys,
In this python script i am going to sync my google calendar events to the script.
I have gone through the setup of the API, I know that it works because if I just use the example script to show the next 10 events as given by Google it works fine.
But what issue i am facing is when i tried to create events it throws me the error.

from __future__ import print_function
import httplib2
import os

from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage

import datetime

try:
   import argparse
   flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
   flags = None

SCOPES = 'https://www.googleapis.com/auth/calendar'
CLIENT_SECRET_FILE = 'Client_Secret.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:
           credentials = tools.run(flow, store)
       print('Storing credentials to ' + credential_path)
   return credentials

def main():

   credentials = get_credentials()
   http = credentials.authorize(httplib2.Http())
   service = discovery.build('calendar', 'v3', http=http)

   GMT_OFF = '+05:30'    
   event = {
   'summary': 'Dinner with friends',
   'start':   {'dateTime': '2017-05-28T19:00:00%s' % GMT_OFF},
   'end':     {'dateTime': '2015-05-28T22:00:00%s' % GMT_OFF},
   }

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




if __name__ == '__main__':
   main()
I cannot get past the following error:

Output:
Traceback (most recent call last):  File "C:\Users\Meeran Rizvi\Documents\To-do-list\quickstart2.py", line 78, in <module>    main()  File "C:\Users\Meeran Rizvi\Documents\To-do-list\quickstart2.py", line 71, in main    event = service.events().insert(calendarId='primary', body=event).execute()  File "C:\Python27\lib\site-packages\oauth2client\_helpers.py", line 133, in positional_wrapper    return wrapped(*args, **kwargs)  File "C:\Python27\lib\site-packages\googleapiclient\http.py", line 840, in execute    raise HttpError(resp, content, uri=self.uri) googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/calendar/v3/calendars/primary/events?alt=json returned "Insufficient Permission"> [Finished in 1.8s]
Reply
#2
The error is saying that you don't have the correct credentials to perform the operation.
you can see from the traceback that when the insert was attempted, it was rejected for lack of
permission.
So maybe when you set that up, it was for read only, or you might have a typo is your 'credentials'

Looking at the example insert here: https://developers.google.com/google-app...nts/insert
it appears that you have to specifically instantiate each event, for example:

instanciate event:
Event event = new Event()
    .setSummary("Google I/O 2015")
    .setLocation("800 Howard St., San Francisco, CA 94103")
    .setDescription("A chance to hear more about Google's developer products.");
set end date and time:
DateTime endDateTime = new DateTime("2015-05-28T17:00:00-07:00");
EventDateTime end = new EventDateTime()
    .setDateTime(endDateTime)
    .setTimeZone("America/Los_Angeles");
event.setEnd(end);
Note that there is a 'new' for each step.
I don't see similar code in your script
Reply
#3
(May-28-2017, 10:04 AM)Larz60+ Wrote:
Event event = new Event()
    .setSummary("Google I/O 2015")
    .setLocation("800 Howard St., San Francisco, CA 94103")
    .setDescription("A chance to hear more about Google's developer products.");
set end date and time:
DateTime endDateTime = new DateTime("2015-05-28T17:00:00-07:00");
EventDateTime end = new EventDateTime()
    .setDateTime(endDateTime)
    .setTimeZone("America/Los_Angeles");
event.setEnd(end);
Note that there is a 'new' for each step.
I don't see similar code in your script
This is not Python code (new ?!) - looks like you've forgotten to select Python tab
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  New on python. Needs help with Google sheet jeromep 1 90 9 hours ago
Last Post: deanhystad
  (Python) Pulling data from UA Google Analytics with more than 100k rows into csv. Stockers 0 1,237 Dec-19-2022, 11:11 PM
Last Post: Stockers
  Deploy Python to Cloud and save output to Google Drive chandrabr80 2 1,584 Jan-25-2022, 06:56 AM
Last Post: ndc85430
  install apache-airflow[postgres,google] on Python 3.8.12 virtual env ShahajaK 1 7,774 Oct-07-2021, 03:05 PM
Last Post: Larz60+
  how to import files in Google Collab wihout downloading them to PC and to Google Disk sveto4ka 9 3,905 Jun-03-2020, 12:40 PM
Last Post: sveto4ka
  PYTHON - GOOGLE... create new spreadsheet? danclark81 3 2,701 Feb-02-2020, 08:57 PM
Last Post: danclark81
  Run School Bell by Google Calendar darkmx6 1 2,021 Oct-29-2019, 02:12 AM
Last Post: Larz60+
  Google API OAuth v2 Python oneclick 0 2,082 Sep-09-2019, 05:02 AM
Last Post: oneclick
  How do I install apps from google play store? using python + selenium.. mongo 0 2,287 Aug-05-2019, 12:41 AM
Last Post: mongo
  Linking python to Google Sheets Charliefish1311 1 2,293 Jul-09-2019, 12:31 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020