在python标准环境快速启动中,端点方法test_api_key返回一个503服务不可用。使用dev运行时,API资源管理器中发生错误_应用程序.py以及部署API时。它的代码是:
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])
我不太了解。获取未识别的\u field_info('key'),因此我不确定问题是什么?谢谢。在
造成503错误的原因有三个。在
首先,我需要使方法或整个Api require an Api Key。在本例中,我将其应用于整个Api:
其次,在云控制台中生成Api密钥后,我需要在部署它之前put the Key into the openapi.json file。在
最后,我仍然得到一个验证错误:
^{pr2}$get_无法识别的_field_info()函数returns a tuple of (value, variant)。响应不需要元组,因此我将方法更新为只显示值:
首先,我建议阅读Google Protocol RPC Library Overview,因为它是Google云端点广泛使用的。在
在@端点.方法允许您在API中配置特定方法。配置选项在Google云平台文档中有文档记录,在创建API时使用Cloud Endpoints Frameworks for App Engine,在Defining an API method (@endpoints.method)一节中。在
如果您限制对
test/getApiKey/test_api_key
方法的访问,那么必须使用api_key_required=True
选项配置该方法。Restricting API Access with API Keys (Frameworks)进一步讨论了这一点,但是您的方法注释应该是:请注意,您的方法接受一个表示HTTP请求的请求参数(即使用API的客户端):
^{pr2}$然而,
request
参数实际上是Google Protocol RPC Message(Proto-RPC)Message对象,因此定义得非常好。如果ProtoRPC请求参数中存在正式定义之外的其他字段,则它们仍与请求对象一起存储,但必须使用以下方法进行检索:Message class code on GitHub有很好的文档记录。在
请求正文中不会出现任何参数,因为您已将方法配置为使用HTTP GET调用:
…您正确地使用了值
message_types.VoidMessage
。在就您的错误而言,503只是一个普通的服务器错误,您能从StackDriver logs提供任何信息吗?它们会指出代码中的确切行和错误。在
相关问题 更多 >
编程相关推荐