【问题标题】:How to use Google Python client in Google Cloud Endpoints?如何在 Google Cloud Endpoints 中使用 Google Python 客户端?
【发布时间】:2015-03-23 08:32:07
【问题描述】:

我正在使用 Google Cloud Endpoints 制作一个 REST API。

我的 API 提供事件服务,例如 Google Calendar Event。

但我的事件模型具有与 Google 日历不同的其他属性。

所以我想将其他属性存储到我的数据存储中。

和其他人存储谷歌日历 API。

顺便说一句,我不知道如何使用 Google API Python 客户端进行 OAuth2 身份验证。

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)

我使用 OAuth2Decorator 但 remote.Service 类没有重定向方法。

所以它会导致错误。

如何在 Google Cloud Endpoints 中使用 Google API Python 客户端。

请告诉我如何验证 OAuth2。

欢迎任何评论,例如文档链接。

【问题讨论】:

    标签: python google-app-engine google-cloud-endpoints google-api-python-client


    【解决方案1】:

    要创建一个 API,其中该 API 的方法需要验证,请通过文档 [1]。您想使用文档 [2] 创建需要身份验证的 API。要在 Google API Python 客户端中使用 OAuth,请阅读文档 [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

    【讨论】:

      【解决方案2】:

      对于服务器到服务器应用程序,您需要使用 OAuth 2.0。由于您使用的是带有 App 引擎的谷歌云端点,因此您可以使用 AppAssertionCredentials 对 Google 日历 API 进行授权 API 调用。下面是修改后的代码:

      from oauth2client.appengine import AppAssertionCredentials
      
      credentials = AppAssertionCredentials('https://www.googleapis.com/auth/calendar')
      http_auth = credentials.authorize(Http())
      calendar_service = build('calendar', 'v3', http=http_auth)
      
      @space_api.api_class(resource_name='events')
      class EventsApi(remote.Service):
      
        @endpoints.method(EVENT_RESOURCE, EventMessage,
                          path='events', http_method='POST',
                          name='events.insert')
        def events_insert(self, request):
          event = service.events().insert(calendarId='primary', body=encode_message(request)).execute(http_auth)
          return decode_message(EventMessage, event)
      

      您可以在此处找到更多详细信息:

      https://developers.google.com/api-client-library/python/auth/service-accounts#callinganapi

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-14
        • 2014-05-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-03
        相关资源
        最近更新 更多