【问题标题】:Using other Google API's in a Google cloud endpoints project (Python)在 Google 云端点项目 (Python) 中使用其他 Google API
【发布时间】:2015-01-27 04:53:49
【问题描述】:

我正在使用 Python 中的 Google Cloud 后端项目来支持 Android 应用程序。我希望服务器能够对用户的帐户进行一些 API 调用 (如联系人、地图等)而不是让 Android 客户端承担此开销。 我根本没有使用 Webapp。我宁愿不这样做 - 我看不出它本身的需要,除非这是完成这项工作的唯一方法。

想知道我将如何前进:

@endpoints.api(name='data_api',
           version='v1',
           description='Access, create or delete data for meetups.',
           audiences=client_ids.allowed_client_ids,
           scopes=[  # Get email and Details
                     endpoints.EMAIL_SCOPE,
                     # Get Contacts
                     client_ids.CONTACTS_SCOPE
           ])
class DataApi(remote.Service):
@UserModel.method(name='user.fetch',
                  user_required=True)
def get_all_contacts(self, query):
    #Now what?

我想需要将 Auth 令牌中继到从服务器到 Google API 的另一个自定义请求上,但我不知道如何提取它并将其向前推进。在文档中找不到它或在此处有任何问题。

感谢您的帮助。

编辑: 我确实知道如何检查用户是否经过身份验证,但这并没有告诉我如何使用相同的身份验证令牌访问 Google API。

if endpoints.get_current_user() is not None:
    #Do secret authenticated business...

【问题讨论】:

    标签: python google-app-engine google-maps-api-3 google-cloud-endpoints google-contacts-api


    【解决方案1】:

    OAuth2 流程的最后一步,检索到的令牌位于 API 请求标头中。我们可以这样访问它:

    import os
    self.bearer = os.getenv('HTTP_AUTHORIZATION')
    

    这提供了可以中继以命中其他 API 的不记名字符串。使用正确的范围,当使用 atom 和 gdata Python 库完成时,我们需要将其装扮成一个新的对象:https://github.com/ca9/meetup-backend/blob/master/atom/auth.py

    在 EndpointsAuth 类中。

    与 gdata 一起使用如下:

    if e_user:
            gd_client = gdata.contacts.client.ContactsClient(source='<var>intense-terra-821</var>', auth_token=EndpointsAuth())
    

    在任何端点函数下。

    【讨论】:

      猜你喜欢
      • 2013-09-21
      • 2018-01-12
      • 2015-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-09
      • 2014-11-10
      相关资源
      最近更新 更多