Insufficient Permission with Google calendar api v3 using python

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()
    
asked by anonymous 27.07.2015 / 17:07

1 answer

2

It's probably been a while since you've asked and maybe even solved it, but I went through that mistake and found the solution.

If you look at documentation you will see that there is a line there in the example they make available which reads as follows:

  

Change the scope to ' link ' and delete anystored credentials.

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

P.S.: There is a line that opens the directory you commented on, decompose it!

    
15.01.2016 / 18:42