Google Cloud Endpoints Python快速入门回声示例问题
在python标准环境快速入门中,端点方法test_api_key返回503服务不可用。在使用dev_appser.py运行时以及部署API时,API Explorer中会发生错误。它的代码是:Google Cloud Endpoints Python快速入门回声示例问题
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(“键”)有很好的理解,所以我不知道是什么问题?谢谢。
有三件事情正在创造503错误。
首先,我需要使方法或整个Api require an Api Key。在这种情况下,我只是把它应用到整个API:
@endpoints.api(name='practice', version='v1', api_key_required=True)
class PracticeApi(remote.Service):
其次,我产生了我需要put the Key into the openapi.json file部署之前的云主机API密钥之后。
最后,我仍然得到验证错误:
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])
首先,我建议您阅读Google Protocol RPC Library Overview,因为它是Google Cloud Endpoints广泛使用的。
@ endpoints.method允许您在API中配置特定的方法。配置选项记录在Google Cloud Platform文档中使用Cloud Endpoints Frameworks for App Engine创建API,部分Defining an API method (@endpoints.method)。
如果您限制访问test/getApiKey/test_api_key
方法,则必须使用选项配置方法。 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的任何信息?他们会指出您的代码中的确切行和错误。
感谢您的详细的答案。我为项目生成了一个API密钥,但不知道如何通过API Explorer传递它,因为它是test_api_key方法所必需的。来自StackDriver的503错误消息是:'ValidationError:Expected type
您是否在使用https://apis-explorer.appspot.com/apis-explorer/?base的API资源管理器= [...]'?如果是这样,您是否授权使用OAuth来设置密钥的帐户?如果您使用的是curl,请传递[https://cloud.google.com/endpoints/docs/restricting-api-access-with-api-keys-frameworks#calling_an_api_using_an_api_key](API密钥记录在案)。 – Jack
感谢您的答复。他们很有帮助,并引导我回到上面发布的答案。 – Nicholas
在文件(openapi.json)中的哪个位置放置了api密钥?谢谢 –
正如我限制所有的方法,我会在[本文档](https://cloud.google.com/endpoints/docs/restricting-api-access-with-api-keys-openapi)中添加。把它放在文件的顶层,而不是嵌套在方法info中。 – Nicholas