Nelfoftheden
Nelfoftheden

Reputation: 56

Using Google Calendar API with an API-key Python

I am trying to integrate google calendar into my web app without using OAuth. Since all calendars that I am working with are public, I am trying to use an API-key instead, to avoid having my users redirected.

I am currently playing around with the Calendar Event example and trying to get it to work with an API-key. Here is the code:

import httplib2
import os

from apiclient import discovery


import datetime



SCOPES = 'https://www.googleapis.com/auth/calendar'
api_key = '************'
APPLICATION_NAME = 'Google Calendar API Quickstart'


def main():

    service = discovery.build('calendar', 'v3', developerKey=api_key)

    event = {
  'summary': 'Google I/O 2015',
  'location': '800 Howard St., San Francisco, CA 94103',
  'description': 'A chance to hear more about Google\'s developer products.',
  'start': {
    'dateTime': '2015-05-28T09:00:00-07:00',
    'timeZone': 'America/Los_Angeles',
  },
  'end': {
    'dateTime': '2015-05-28T17:00:00-07:00',
    'timeZone': 'America/Los_Angeles',
  },
  'recurrence': [
    'RRULE:FREQ=DAILY;COUNT=2'
  ],
  'attendees': [
    {'email': '******@*****.com'},

  ],
  'reminders': {
    'useDefault': False,
    'overrides': [
      {'method': 'email', 'minutes': 24 * 60},
      {'method': 'popup', 'minutes': 10},
    ],
  },
}

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


if __name__ == '__main__':
    main()

When this runs it gives me the following error: googleapiclient.errors.HttpError: https://www.googleapis.com/calendar/v3/calendars/primary/events?alt=json&key=********* returned "Login Required">

Any help would be much appreciated

Upvotes: 4

Views: 3692

Answers (3)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116868

You can only use an API key to read from a public calendar. I have never insert or update work.

What you should be useing is a service account which is like a dummy pre-approved user with its own calendar account. you can also share calendars with it. it will then have access to read and write to these calendars without promoting your user's for access.

I am not a python dev so can't help much with code but there is lots of example of it in Google's documentation

Upvotes: 0

GordanTrevis
GordanTrevis

Reputation: 94

I am using Python 3 to get the upcoming 10 events on a Public Calendar only by using the API Developer Key - without OAuth 2.

Without from apiclient.discovery import build it also does not work.

This Works for me (Only tested in console):

from apiclient import discovery
import datetime
from apiclient.discovery import build


dev_Key = "XXXX"
cal_ID = "[email protected]"
now = datetime.datetime.utcnow().isoformat() + 'Z'

service = build('calendar', 'v3', developerKey=dev_Key)


eventsResult = service.events().list(calendarId=cal_ID, timeMin=now, maxResults=10, singleEvents=True, orderBy='startTime').execute()
events = eventsResult.get('items', [])

    for event in events:
        start = event['start'].get('dateTime', event['start'].get('date'))
        print(start, event['summary'])

Upvotes: 3

Martin Lear
Martin Lear

Reputation: 262

Sorry, you will have to use OAuth 2

"Your application must use OAuth 2.0 to authorize requests. No other authorization protocols are supported. "

Upvotes: 0

Related Questions