2

I want to use Google Calendar API in my Django web app.

I successfully got the access token using python social auth.

Using that token, I tried calling list calendar API with python requests.

Below is my code:

import requests

token = ...

endpoint = "https://content.googleapis.com/calendar/v3/users/me/calendarList"

headers = {
    "Authorization":"Bearer " + token,
}

requests.get(endpoint,headers=headers).json()

However, I got the following response:

u'error': 
    u'code': 403, 
    u'message': u'Insufficient Permission', 
    u'errors': 
        u'domain': u'global', 
        u'message': u'Insufficient Permission', 
        u'reason': u'insufficientPermissions'

How can this be solved? I really want to solve this problem without using any other libraries including API Client Library.

upoque
  • 53
  • 8
  • How are you getting your token? – AChampion Jul 09 '18 at 20:20
  • @AChampion Using python social app `def get_access_token(request):` `social = request.user.social_auth.get(provider='google-oauth2')` `return social.extra_data['access_token']` – upoque Jul 09 '18 at 20:31
  • The authorization backend which I'm using is [this](https://github.com/python-social-auth/social-core/blob/master/social_core/backends/google.py) – upoque Jul 09 '18 at 20:37
  • 1
    I'm assuming that you have given the original access token the right scopes to access calendar? – AChampion Jul 09 '18 at 20:54
  • @AChampion I think I missed that part. Can you tell me how I could grant calendar access to the token? (with python requests or python social auth if possible) – upoque Jul 09 '18 at 21:14
  • Can you post your `python social auth` code? Assuming the project/client token has the calender api enabled then you need to pass a scope parameter through the social auth, so you get `?scope=https://www.googleapis.com/auth/calendar` appropriately encoded on the auth request. – AChampion Jul 09 '18 at 23:38
  • 1
    A quick check on `python social auth` docs seems you need to define `SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPE = ['https://www.googleapis.com/auth/calendar']` – AChampion Jul 09 '18 at 23:41
  • Thank you! Setting `SOCIAL_AUTH_GOOGLE_OAUTH2_SCOPE` solved the problem. – upoque Jul 10 '18 at 03:43

0 Answers0