0

I'm using django-allauth to log users in and get a view of their calendars and I want to be able to add appointments.

I'm using the following code which is more or less a cut and paste from quickstart.py:

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
from oauth2client.client import OAuth2WebServerFlow
from apiclient.discovery import build

import datetime

from django.shortcuts import render

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

# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/calendar-python-quickstart.json
SCOPES = 'https://www.googleapis.com/auth/calendar'
CLIENT_SECRET_FILE = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+'/cal/client_secret.json'
APPLICATION_NAME = 'Google Calendar API Python Quickstart'

def get_credentials():
    """Gets valid user credentials from storage.

    If nothing has been stored, or if the stored credentials are invalid,
    the OAuth2 flow is completed to obtain the new credentials.

    Returns:
        Credentials, the obtained credential.
    """
    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

def set_appointment(request):
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    CAL = discovery.build('calendar', 'v3', http=http)

    GMT_OFF = '+00:00'      # PDT/MST/GMT-7
    EVENT = {
        'summary': 'Dinner with friends',
        'start':  {'dateTime': '2016-11-04T19:00:00%s' % GMT_OFF},
        'end':    {'dateTime': '2016-11-04T22:00:00%s' % GMT_OFF},
        #'attendees': [
        #    {'email': 'friend1@outlook.com'},
        #    {'email': 'friend2@hotmail.com'},
        #],
    }

    e = CAL.events().insert(calendarId='primary',
            sendNotifications=True, body=EVENT).execute()

    return render(request, 'cal/index.html')

But I get the following error when trying to add an event:

<HttpError 403 when requesting https://www.googleapis.com/calendar/v3/calendars/primary/events?alt=json&sendNotifications=true returned "Insufficient Permission">

I thought maybe changing the SCOPE for django-allauth would do the trick and added this to settings.py

SOCIALACCOUNT_PROVIDERS = \
    { 'google':
        { 'SCOPE': ['profile', 'email', 'https://www.googleapis.com/auth/calendar'],
            'AUTH_PARAMS': { 'access_type': 'online' } }}

It's not worked. Help would be greatly appreciated.

HenryM
  • 5,557
  • 7
  • 49
  • 105

0 Answers0