Insufficient Permission with Google Calendar api v3 using python

Asked

Viewed 168 times

2

I’m not getting permissions to create the event.

Below the code I’m using:

import httplib2
import os
import json
from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools

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 = 'Insert Event'


def get_credentials():
    "Gera as crendeciais"
    #home_dir = os.path.expanduser('~')
    credential_dir = os.path.join('credentials', '.credentials')
    if not os.path.exists(credential_dir):
    os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir, 'calendar.json')

    store = oauth2client.file.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 compatability with Python 2.6
            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)


    title = 'teste de insercao'
    day = '27'
    month = '07'
    year = '2015'
    hourstart = '20'
    minutestart = '00'
    hourfinal = '20'
    minutefinal = '30'

    event = {
      'summary': title,
      'start': {
        'dateTime': year + '-'+ month + '-'+ day + 'T' + hourstart + ':'+        minutestart +':00-03:00',
        'timeZone': 'America/Sao_Paulo',
       },
      'end': {
        'dateTime': year + '-'+ month + '-'+ day + 'T' + hourfinal + ':'+ minutefinal +':00-03:00',
        'timeZone': 'America/Sao_Paulo',
      },
      'recurrence': [
        'RRULE:FREQ=MONTHLY;COUNT=3'
      ],
      'attendees': [
        {'email': '[email protected]'},
      ],
      'reminders': {
        'useDefault': False,
        'overrides': [
          {'method': 'email', 'minutes': 24 * 60},
          {'method': 'popup', 'minutes': 10},
        ],
      },
    }
    create_event = service.events().insert(calendarId='primary', body=event,   sendNotifications= False).execute()
    print ('foi')

if __name__ == '__main__':
    main()

1 answer

2


It’s probably been a while since you asked and maybe even fixed, but I went through this mistake and found the solution.

If you look at documentation you will see that there is a line there in the example that they provide that says the following:

Change the Scope to 'https://www.googleapis.com/auth/calendar' and delete anystored credentials.

The scope you changed, but what’s missing is you go to your home directory
(of the kind: C:\Users\seunomedeuser>.credentials) and delete the folder .credentials, because you changed the access permission.

P.S.: There’s a line that opens the directory you mentioned, take it down!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.