Skip to main content
 首页 » 编程设计

python之如何使用 OAuth 从 Python 应用程序访问 Google App Engine 端点 API

2025年05月04日25haluo1

我如何访问适用于 Python 的 Google App Engine 端点 API(不是 Web、Android、iOS)?

我读了这个tutorial但它不足以解释这一点。

正如我在服务端发现的那样,我可以使用这样的代码来识别用户:

@endpoints.method(message_types.VoidMessage, Greeting, 
                  path='hellogreeting/authed', http_method='POST', 
                  name='greetings.authed') 
def greeting_authed(self, request): 
    current_user = endpoints.get_current_user() 
    email = (current_user.email() if current_user is not None 
             else 'Anonymous') 
    return Greeting(message='hello %s' % (email,)) 

Full code of API example

我如何从 Python 客户端连接到此 API 并使用身份验证 current_user != None 调用“hellogreeting/authed”。

你能分享一些代码吗?

app_id = 'xxx' 
user = 'xxx' 
password = 'xxx' 
callAPI(app_id, user, password, 'hellogreeting/authed') 

请您参考如下方法:

您需要配置 App Engine 实例才能为您的 API 提供服务。我建议您创建一个专用于您的 API 的单独模块,如这些文档中所述:https://developers.google.com/appengine/docs/python/endpoints/api_server。

在服务器端正确设置所有内容后,您可以使用类似以下内容调用 API:http://your-module.your-app.appspot.com/_ah/spi/hellogreeting/authed。

如果您使用的是开发服务器,那么访问模块的方式会略有不同,但是一旦您知道 App Engine 开发服务器为您的 API 模块分配了哪个端口号,您就可以使用 http://localost:<api_module_port_#>/_ah/spi/hellogreeting/authed 在本地访问它。

希望这对您有所帮助。