Google Cloud Endpoints Python 快速入门 echo 示例问题
Google Cloud Endpoints Python Quickstart echo sample issue
在 python 标准环境快速入门中,端点方法 test_api_key returns 503 服务不可用。当 运行 和 dev_appser.py 以及部署 API 时,API Explorer 中会出现错误。它的代码是:
import endpoints
from protorpc import message_types
from protorpc import messages
from protorpc import remote
class TestResponse(messages.Message):
content = messages.StringField(1)
@endpoints.api(name='practice', version='v1', description='My Practice API')
class PracticeApi(remote.Service):
@endpoints.method(
message_types.VoidMessage,
TestResponse,
path='test/getApiKey',
http_method='GET',
name='test_api_key')
def test_api_key(self, request):
return TestResponse(content=request.get_unrecognized_field_info('key'))
api = endpoints.api_server([PracticeApi])
我不太了解 .get_unrecognized_field_info('key') 所以我不确定问题出在哪里?谢谢
首先,我推荐阅读 Google Protocol RPC Library Overview,因为它 Google Cloud Endpoints 广泛使用它。
@endpoints.method 允许您在 API 中配置特定方法。配置选项记录在 Google Cloud Platform 文档中 Creating an API with Cloud Endpoints Frameworks for App Engine, in the section, Defining an API method (@endpoints.method).
如果您要限制对 test/getApiKey/test_api_key
方法的访问,则必须使用 api_key_required=True
选项配置该方法。 Restricting API Access with API Keys (Frameworks) 进一步讨论,但您的方法注释应该是:
@endpoints.method(
message_types.VoidMessage,
TestResponse,
path='test/getApiKey',
http_method='GET',
name='test_api_key',
api_key_required=True
)
请注意您的方法接受代表 HTTP 请求的请求参数(即客户端使用您的 API):
def test_api_key(self, request):
但是,request
参数实际上是 Google Protocol RPC Message (Proto RPC) Message 对象,因此定义得很好。如果 ProtoRPC 请求参数中存在其他字段,超出正式定义的内容,它们仍与请求对象一起存储,但必须使用以下方法检索:
def get_unrecognized_field_info(self, key, value_default=None,
variant_default=None):
"""Get the value and variant of an unknown field in this message.
Args:
key: The name or number of the field to retrieve.
value_default: Value to be returned if the key isn't found.
variant_default: Value to be returned as variant if the key isn't
found.
Returns:
(value, variant), where value and variant are whatever was passed
to set_unrecognized_field.
"""
Message class code on GitHub 有很好的记录。 .
请求正文中不会出现任何参数,因为您已将方法配置为使用 HTTP GET 调用:
http_method='GET'
...您正确使用了值 message_types.VoidMessage
。
关于你的错误,503只是一个一般的服务器错误,你能提供一些来自StackDriver logs的信息吗?他们会指出代码中的确切行和错误。
造成 503 错误的原因有三。
首先,我需要制作方法或整个 Api require an Api Key。在这种情况下,我只是将它应用于整个 Api:
@endpoints.api(name='practice', version='v1', api_key_required=True)
class PracticeApi(remote.Service):
其次,在云控制台中生成 Api 密钥后,我需要 put the Key into the openapi.json file 才能部署它。
最后,我仍然收到验证错误:
ValidationError: Expected type <type 'unicode'> for field content, found (u'My Api Key', Variant(STRING, 9)) (type <type 'tuple'>)
get_unrecognized_field_info() 函数returns a tuple of (value, variant)。响应不期望元组,因此我更新了方法以仅显示值:
def test_api_key(self, request):
return TestResponse(content=request.get_unrecognized_field_info('key')[0])
在 python 标准环境快速入门中,端点方法 test_api_key returns 503 服务不可用。当 运行 和 dev_appser.py 以及部署 API 时,API Explorer 中会出现错误。它的代码是:
import endpoints
from protorpc import message_types
from protorpc import messages
from protorpc import remote
class TestResponse(messages.Message):
content = messages.StringField(1)
@endpoints.api(name='practice', version='v1', description='My Practice API')
class PracticeApi(remote.Service):
@endpoints.method(
message_types.VoidMessage,
TestResponse,
path='test/getApiKey',
http_method='GET',
name='test_api_key')
def test_api_key(self, request):
return TestResponse(content=request.get_unrecognized_field_info('key'))
api = endpoints.api_server([PracticeApi])
我不太了解 .get_unrecognized_field_info('key') 所以我不确定问题出在哪里?谢谢
首先,我推荐阅读 Google Protocol RPC Library Overview,因为它 Google Cloud Endpoints 广泛使用它。
@endpoints.method 允许您在 API 中配置特定方法。配置选项记录在 Google Cloud Platform 文档中 Creating an API with Cloud Endpoints Frameworks for App Engine, in the section, Defining an API method (@endpoints.method).
如果您要限制对 test/getApiKey/test_api_key
方法的访问,则必须使用 api_key_required=True
选项配置该方法。 Restricting API Access with API Keys (Frameworks) 进一步讨论,但您的方法注释应该是:
@endpoints.method(
message_types.VoidMessage,
TestResponse,
path='test/getApiKey',
http_method='GET',
name='test_api_key',
api_key_required=True
)
请注意您的方法接受代表 HTTP 请求的请求参数(即客户端使用您的 API):
def test_api_key(self, request):
但是,request
参数实际上是 Google Protocol RPC Message (Proto RPC) Message 对象,因此定义得很好。如果 ProtoRPC 请求参数中存在其他字段,超出正式定义的内容,它们仍与请求对象一起存储,但必须使用以下方法检索:
def get_unrecognized_field_info(self, key, value_default=None,
variant_default=None):
"""Get the value and variant of an unknown field in this message.
Args:
key: The name or number of the field to retrieve.
value_default: Value to be returned if the key isn't found.
variant_default: Value to be returned as variant if the key isn't
found.
Returns:
(value, variant), where value and variant are whatever was passed
to set_unrecognized_field.
"""
Message class code on GitHub 有很好的记录。 .
请求正文中不会出现任何参数,因为您已将方法配置为使用 HTTP GET 调用:
http_method='GET'
...您正确使用了值 message_types.VoidMessage
。
关于你的错误,503只是一个一般的服务器错误,你能提供一些来自StackDriver logs的信息吗?他们会指出代码中的确切行和错误。
造成 503 错误的原因有三。
首先,我需要制作方法或整个 Api require an Api Key。在这种情况下,我只是将它应用于整个 Api:
@endpoints.api(name='practice', version='v1', api_key_required=True)
class PracticeApi(remote.Service):
其次,在云控制台中生成 Api 密钥后,我需要 put the Key into the openapi.json file 才能部署它。
最后,我仍然收到验证错误:
ValidationError: Expected type <type 'unicode'> for field content, found (u'My Api Key', Variant(STRING, 9)) (type <type 'tuple'>)
get_unrecognized_field_info() 函数returns a tuple of (value, variant)。响应不期望元组,因此我更新了方法以仅显示值:
def test_api_key(self, request):
return TestResponse(content=request.get_unrecognized_field_info('key')[0])