1

@peter-hudec

Reusing the same thread for a new problem encountered. I can only make one PUT/POST/GET request with each login. I can't reuse the login 'result' object.

The first time I make a request to get all events in Google Calendar. I check whether my own events exist in Google Calendar. If it doesn't exist, add them to Google Calendar.

The code below works in separate pages but not in one single login session in the same page.

provider_name = 'google'
response = make_response()
print  'Response', response
result = authomatic_inst.login(WerkzeugAdapter(request, response), provider_name)
print 'Result', (result)

if result:
    if result.user:
        # Get user info
        result.user.update()

# Talk to Google Calendar API
        if result.user.credentials:
            response = result.provider.access('https://www.googleapis.com/calendar/v3/calendars/<CALENDARID>/events?key=<YOUR_API_KEY>', method='GET')
            if response.status == 200:
                items = response.data.get('items', {})

                ### <code to Check whether event exists in Google Calendar before adding it in>
                # IF event not in Google Calendar, add it 

                    body_json = json.dumps(event)
                    response = result.provider.access('https://www.googleapis.com/calendar/v3/calendars/<CALENDARID>/events?key=<YOUR_API_KEY>', method='POST', body=body_json, headers={'Content-Type':'application/json;charset=UTF-8'})
                    if response.status == 200:
                        res = response.data
                    return json.dumps(res)

Old question below:

I'm using Authomatic for managing OAuth2.0 logins.

I followed this [answer][1] posted by the creator. It works for YouTube authorisation, but not for Google Calendar.

http://peterhudec.github.io/authomatic/reference/classes.html#authomatic.Authomatic.access

if result:
        if result.user:
            # Get user info
            result.user.update()
 # Talk to Google Calendar API
            if result.user.credentials:
                response = result.provider.access('https://www.googleapis.com/calendar/v3/calendars/<calendarid>/events?key=<authkey>',

method='GET') if response.status == 200: print response

return response

The [GET request][2] with an interactive example as provided by Google Calendar API is:

GET https://www.googleapis.com/calendar/v3/calendars/<CALENDARID>/events?key={YOUR_API_KEY}
Authorization:  Bearer ya29.BwGoAyc8yqYGzDz3FEPn-_zYU_EFLy0hiQzbv1h9zOnzlJe4dw1q68WNLuW7weJKNjtYYcy3P_AbsA
X-JavaScript-User-Agent:  Google APIs Explorer

I'm pretty sure I'm using the same request but I'm getting error code 403 Forbidden?

[1]: https://stackoverflow.com/a/21987075/3583980 [2]: https://developers.google.com/google-apps/calendar/v3/reference/events/list

Community
  • 1
  • 1
riddler
  • 467
  • 3
  • 13
  • What are you putting as ``? – Daniel Roseman Jan 26 '15 at 15:24
  • 'primary' as the Python client library used. It worked with the Python client library then I switched to REST to integrate with the rest of the project as the two login types were clashing. – riddler Jan 26 '15 at 15:41
  • Did you get same 403 error when you tried in API explorer provided by google? – SGC Jan 26 '15 at 17:09
  • No I don't. Fixed the error, the primary calendar is called your Google Account email, not called 'primary'. – riddler Jan 27 '15 at 16:44

1 Answers1

1

The authomatic.login() method merely acquires for you an access token of the user which is contained in result.user.credentials. You can then use the credentials to access the provider API:

response = authomatic.access(result.user.credentials, 'https://example.com/api')
# Following uses the code above internally
response = result.provider.access('https://example.com/api')

You don't need to go through the login procedure each time you want to make an API request. I would recommend to move the API calls to some different view:

def login_view(request):
    response = HttpResponse()
    result = authomatic.login(DjangoAdapter(request, response),
                              provider_name)
    if result:
        if result.credentials:
            # You can serialize the credentials to move them across requests.
            # The best would be to store them to the DB with the user.
            request.session['credentials'] = result.credentials.serialize()
        return redirect('api-view')
    return response


def api_view(request):
    serialized_credentials = request.get('credentials')
    if serialized_credentials:

        # You can deserialize the credentials...
        credentials = authomatic.credentials(serialized_credentials)

        # ...and update them if they are about to expire soon
        # Google access tokens are valid for 6 months
        if credentials.expire_soon():
            response = credentials.refresh()
            if response.status == 200:
                request.session['credentials'] = credentials.serialize()

        # The access method accepts both serialized and
        # deserialized credentials.
        response = authomatic.access(credentials, 'https://example.com/api')

You can read more about the credentials in the docs. There is also a Webapp2 based tutorial available which is easily portable to Django.

Peter Hudec
  • 2,462
  • 3
  • 22
  • 29