【问题标题】:Consuming GAE Endpoints with a Python client使用 Python 客户端使用 GAE 端点
【发布时间】:2014-02-05 05:07:46
【问题描述】:

我正在使用 Google AppEngine Endpoints 构建 Web API。 我将使用用 Python 编写的客户端来使用它。 我知道提供了脚本来生成 Android 和 iOS 客户端 API,但似乎没有任何可与 Python 相媲美的东西。

重新编写所有代码似乎是多余的。比如messages的定义基本一致。

有没有更容易做到这一点?

谢谢

【问题讨论】:

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


    【解决方案1】:

    您可以使用与端点兼容的Google APIs Client Library for Python。

    通常您会使用service = build(api, version, http=http) 构建客户端,例如service = build("plus", "v1", http=http) 来构建客户端以访问 Google+ API。

    为了将库用于您的端点,您将使用:

    service = build("your_api", "your_api_version", http=http, 
      discoveryServiceUrl=("https://yourapp.appspot.com/_ah/api/discovery/v1/"
                           "apis/{api}/{apiVersion}/rest"))
    

    然后您可以使用

    访问您的 API
    result = service.resource().method([parameters]).execute()
    

    【讨论】:

    • 哇,非常感谢。我明天会测试这个,然后回来给你我的结果:)
    • 确保检查客户端库的文档:developers.google.com/api-client-library/python/start/… 基本上,他们为 Google API 描述的所有内容也适用于您的端点(如果需要,包括身份验证)
    • 我终于有时间试一试了。我从构建中删除了http=http。我想在本地尝试,但无法在 GAE 开发服务器上启用 https,即使我将发现 url 设置为 http,它也会切换到 https 以发出任何不起作用的请求。我可以将它部署到 GAE,但由于我无法直接编辑我的数据存储,因此不太实用。所以它确实有效,但我猜它对于开发环境并不实用。再次感谢。
    【解决方案2】:

    以下是端点 helloworld 问候示例发生的情况:

    __author__ = 'robertking'
    
    import httplib2
    from apiclient.discovery import build
    
    http = httplib2.Http()
    
    service = build("helloworld", "v1", http=http,
      discoveryServiceUrl=("http://localhost:8080/_ah/api/discovery/v1/apis/helloworld/v1/rest"))
    
    print service.greetings().listGreeting().execute()['items']
    
    """
    prints
    [{u'message': u'hello world!'}, {u'message': u'goodbye world!'}]
    """
    

    现在我正在使用 http。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-22
      • 2021-12-06
      • 1970-01-01
      相关资源
      最近更新 更多