We're trying to use the Google Calendars API in our Django project, when I try python manage.py runserver
I get a 404 error, and when I click the link it provides me I get this:
{
"error": {
"code": 403,
"message": "The request is missing a valid API key.",
"errors": [
{
"message": "The request is missing a valid API key.",
"domain": "global",
"reason": "forbidden"
}
],
"status": "PERMISSION_DENIED"
}
}
I went to the developer console and made an API key, but I'm not really sure how to use it. I came across someone using the API key as part of the "context" variable when making a POST request, but I haven't even attempted submitting the form that is supposed to call the API; I'm getting this error on startup. I tried adding a key: [my API key] in my service account json file, but that did not fix anything.
Here is the Calendars code we have:
BASE_DIR = os.getcwd()
CLIENT_SECRET_FILE = BASE_DIR + '\calendar_api\credentials.json' # 'calender_key.json'
SCOPES = 'https://www.googleapis.com/auth/calendar'
scopes = [SCOPES]
APPLICATION_NAME = 'Google Calendar API Python'
class google_calendar_api:
def build_service(self):
credentials = ServiceAccountCredentials.from_json_keyfile_name(
CLIENT_SECRET_FILE,
SCOPES
)
http = credentials.authorize(httplib2.Http())
service = build('calendar', 'v3', http=http, cache_discovery=False)
return service
def create_event(self, calendar_id, start, end, desc, ):
service = self.build_service()
event = service.events().insert(calendarId=calendar_id, body={
'description':desc,
'summary':desc,
'start':{'dateTime': start},
'end':{'dateTime': end},
}).execute()
return event['id']
def update_event(self,calendar_id, event_id, start, end, desc):
service = self.build_service()
try:
event = service.events().get(calendarId=calendar_id, eventId=event_id).execute()
except HttpError as e:
if e.resp.status==404:
return self.create_event(calendar_id, start, end, desc)
event["start"]={'dateTime':start}
event["end"]={'dateTime':end}
event["summary"]= desc
event["description"]= desc
updated_event = service.events().update(calendarId=calendar_id, eventId=event['id'], body=event).execute()
return updated_event["id"]