Search code examples
pythongoogle-app-enginegoogle-cloud-endpointsgoogle-api-python-client

How to use Google Python client in Google Cloud Endpoints?


I'm making a REST API using Google Cloud Endpoints.

My API provides event service such as Google Calendar Event.

But my event model has additional properties unlike Google Calendar.

so I want to store additional properties into my datastore.

and the others store Google Calendar API.

by the way I don't know how to OAuth2 authenticate for using Google API Python Client.

decorator = OAuth2Decorator(
    client_id='',
    client_secret='',
    scope='https://www.googleapis.com/auth/calendar')

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

@space_api.api_class(resource_name='events')
class EventsApi(remote.Service):

  @endpoints.method(EVENT_RESOURCE, EventMessage,
                    path='events', http_method='POST',
                    name='events.insert')
  @decorator.oauth_required
  def events_insert(self, request):
    event = service.events().insert(calendarId='primary', body=encode_message(request)).execute(http=decorator.http())
    return decode_message(EventMessage, event)

I use OAuth2Decorator but remote.Service class has no redirect method.

so it causes an error.

What should I do for using Google API Python client in Google Cloud Endpoints.

Please let me know how to authenticate OAuth2.

Welcome any comment such as documentation link.


Solution

  • To create an API in which a methods for that API need authentication go through the document [1]. You want to create an API which needs authentication use the docs [2]. To use OAuth in Google API Python Client go through the document [3].

    [1] https://cloud.google.com/appengine/docs/python/endpoints/getstarted/backend/auth

    [2] https://cloud.google.com/appengine/docs/python/endpoints/auth

    [3] https://cloud.google.com/appengine/docs/python/endpoints/access_from_python