DNN Forums

Ask questions about your website to get help learning DNN and help resolve issues.

Google Calendars API with Django — Error 403, Missing valid API key

 1 Replies
 0 Subscribed to this topic
 28 Subscribed to this forum
Sort:
Author
Messages
New Around Here
Posts: 1
New Around Here

We're trying to use the Google Calendars API in our Django project, when I try <code>python manage.py runserver</code> I get a 404 error, and when I click the link it provides me I get this:

<code>{
  "error": {
    "code": 403,
    "message": "The request is missing a valid API key.",
    "errors": &#91;
      {
        "message": "The request is missing a valid API key.",
        "domain": "global",
        "reason": "forbidden"
      }
    &#93;,
    "status": "PERMISSION_DENIED"
  }
}
</code>

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:

<code>BASE_DIR = os.getcwd()
CLIENT_SECRET_FILE = BASE_DIR + '\calendar_api\credentials.json'  # 'calender_key.json'
SCOPES = 'https://www.googleapis.com/auth/calendar'
scopes = &#91;SCOPES&#93;
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&#91;'id'&#93;


    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&#91;"start"&#93;={'dateTime':start}
        event&#91;"end"&#93;={'dateTime':end}
        event&#91;"summary"&#93;= desc
        event&#91;"description"&#93;= desc
        updated_event = service.events().update(calendarId=calendar_id, eventId=event&#91;'id'&#93;, body=event).execute()
        return updated_event&#91;"id"&#93;</code>
Senior Member
Posts: 1322
Senior Member
3 Helpful Replier
Helpful Replier
Thanks for being such a helpful replier!
Lifesaver
Lifesaver
You're Life Saver!
New Poster
New Poster
Congrats on posting!
3 Engaged Reader
Engaged Reader
You are an engaged reader!

Can you explain to me how this is related to DNN?

These Forums are for the discussion of the open source CMS DNN platform and ecosystem.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. If you have (suspected) security issues, please DO NOT post them in the forums but instead follow the official DNN security policy
  2. No Advertising. This includes the promotion of commercial and non-commercial products or services which are not directly related to DNN.
  3. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  4. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  5. No Flaming or Trolling.
  6. No Profanity, Racism, or Prejudice.
  7. Site Moderators have the final word on approving / removing a thread or post or comment.
  8. English language posting only, please.

Would you like to help us?

Awesome! Simply post in the forums using the link below and we'll get you started.

Get Involved