Google Cloud Endpoints Python Quickstart echo示例问题

时间:2021-01-17 23:13:08

In the python standard environment quickstart, the endpoints method test_api_key returns a 503 Service Unavailable. The error occurs in the API Explorer when run with dev_appser.py and when the API is deployed. The code for it is:

在python标准环境快速入门中,端点方法test_api_key返回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])

I don't have a good understanding of .get_unrecognized_field_info('key') so I am not sure what the issue is? Thanks.

我对.get_unrecognized_field_info('key')没有很好的理解,所以我不确定是什么问题?谢谢。

2 个解决方案

#1


1  

Firstly, I recommend reading Google Protocol RPC Library Overview, since it's Google Cloud Endpoints uses it extensively.

首先,我建议阅读Google协议RPC库概述,因为它的Google Cloud Endpoints广泛使用它。

@endpoints.method allows you to configure a specific method in your API. Configuration options are documented in Google Cloud Platform documentation Creating an API with Cloud Endpoints Frameworks for App Engine, in the section, Defining an API method (@endpoints.method).

@ endpoints.method允许您在API中配置特定方法。配置选项记录在Google Cloud Platform文档中,使用Cloud Endpoints框架为App Engine创建API,在“定义API方法”(@ endpoints.method)一节中。

If you're restricting access to the test/getApiKey/test_api_key method, then you must configure the method with the api_key_required=True option. Restricting API Access with API Keys (Frameworks) discusses that further, but your method annotation should be:

如果您限制访问test / getApiKey / test_api_key方法,则必须使用api_key_required = True选项配置该方法。使用API​​密钥限制API访问(框架)进一步讨论,但您的方法注释应该是:

@endpoints.method(
        message_types.VoidMessage,
        TestResponse,
        path='test/getApiKey',
        http_method='GET',
        name='test_api_key',
        api_key_required=True
)

Notice your method accepts a request parameter representing the HTTP request (i.e. client using your API):

请注意,您的方法接受表示HTTP请求的请求参数(即使用您的API的客户端):

def test_api_key(self, request):

However, the request parameter is actually Google Protocol RPC Message (Proto RPC) Message object and as such is very well defined. If additional fields exist in the ProtoRPC request parameter, beyond what is formally defined, they are still stored with the request object but must be retrieved using the following method:

但是,请求参数实际上是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 is quite well documented. .

GitHub上的消息类代码已有详细记录。 。

No arguments will appear in the body of a request because you've configured the method with to be called with HTTP GET:

请求正文中不会出现任何参数,因为您已将方法配置为使用HTTP GET调用:

http_method='GET'

...you're correctly using the value message_types.VoidMessage.

...您正确使用值message_types.VoidMessage。

In terms of your error, 503 is just a generic server error, can you provide any information from the StackDriver logs? They will point you to the exact line and error in your code.

就您的错误而言,503只是一个通用的服务器错误,您能提供StackDriver日志中的任何信息吗?它们将指向您的代码中的确切行和错误。

#2


0  

There were three things that were creating the 503 error.

创建503错误有三件事。

Firstly, I needed to make the method or entire Api require an Api Key. In this case I just applied it to the entire Api:

首先,我需要制作方法或整个Api需要Api Key。在这种情况下,我只是将它应用于整个Api:

@endpoints.api(name='practice', version='v1', api_key_required=True)
class PracticeApi(remote.Service):

Secondly, after I generated the Api Key in the cloud console I needed to put the Key into the openapi.json file before deploying it.

其次,在云控制台中生成Api密钥之后,我需要在部署之前将密钥放入openapi.json文件中。

Lastly, I was still getting a validation error:

最后,我仍然收到验证错误:

ValidationError: Expected type <type 'unicode'> for field content, found (u'My Api Key', Variant(STRING, 9)) (type <type 'tuple'>)

The get_unrecognized_field_info() function returns a tuple of (value, variant). A tuple was not expected by the response so I updated the method to only show value:

get_unrecognized_field_info()函数返回(value,variant)的元组。响应没有预期元组,所以我更新了方法只显示值:

    def test_api_key(self, request):
        return TestResponse(content=request.get_unrecognized_field_info('key')[0])

#1


1  

Firstly, I recommend reading Google Protocol RPC Library Overview, since it's Google Cloud Endpoints uses it extensively.

首先,我建议阅读Google协议RPC库概述,因为它的Google Cloud Endpoints广泛使用它。

@endpoints.method allows you to configure a specific method in your API. Configuration options are documented in Google Cloud Platform documentation Creating an API with Cloud Endpoints Frameworks for App Engine, in the section, Defining an API method (@endpoints.method).

@ endpoints.method允许您在API中配置特定方法。配置选项记录在Google Cloud Platform文档中,使用Cloud Endpoints框架为App Engine创建API,在“定义API方法”(@ endpoints.method)一节中。

If you're restricting access to the test/getApiKey/test_api_key method, then you must configure the method with the api_key_required=True option. Restricting API Access with API Keys (Frameworks) discusses that further, but your method annotation should be:

如果您限制访问test / getApiKey / test_api_key方法,则必须使用api_key_required = True选项配置该方法。使用API​​密钥限制API访问(框架)进一步讨论,但您的方法注释应该是:

@endpoints.method(
        message_types.VoidMessage,
        TestResponse,
        path='test/getApiKey',
        http_method='GET',
        name='test_api_key',
        api_key_required=True
)

Notice your method accepts a request parameter representing the HTTP request (i.e. client using your API):

请注意,您的方法接受表示HTTP请求的请求参数(即使用您的API的客户端):

def test_api_key(self, request):

However, the request parameter is actually Google Protocol RPC Message (Proto RPC) Message object and as such is very well defined. If additional fields exist in the ProtoRPC request parameter, beyond what is formally defined, they are still stored with the request object but must be retrieved using the following method:

但是,请求参数实际上是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 is quite well documented. .

GitHub上的消息类代码已有详细记录。 。

No arguments will appear in the body of a request because you've configured the method with to be called with HTTP GET:

请求正文中不会出现任何参数,因为您已将方法配置为使用HTTP GET调用:

http_method='GET'

...you're correctly using the value message_types.VoidMessage.

...您正确使用值message_types.VoidMessage。

In terms of your error, 503 is just a generic server error, can you provide any information from the StackDriver logs? They will point you to the exact line and error in your code.

就您的错误而言,503只是一个通用的服务器错误,您能提供StackDriver日志中的任何信息吗?它们将指向您的代码中的确切行和错误。

#2


0  

There were three things that were creating the 503 error.

创建503错误有三件事。

Firstly, I needed to make the method or entire Api require an Api Key. In this case I just applied it to the entire Api:

首先,我需要制作方法或整个Api需要Api Key。在这种情况下,我只是将它应用于整个Api:

@endpoints.api(name='practice', version='v1', api_key_required=True)
class PracticeApi(remote.Service):

Secondly, after I generated the Api Key in the cloud console I needed to put the Key into the openapi.json file before deploying it.

其次,在云控制台中生成Api密钥之后,我需要在部署之前将密钥放入openapi.json文件中。

Lastly, I was still getting a validation error:

最后,我仍然收到验证错误:

ValidationError: Expected type <type 'unicode'> for field content, found (u'My Api Key', Variant(STRING, 9)) (type <type 'tuple'>)

The get_unrecognized_field_info() function returns a tuple of (value, variant). A tuple was not expected by the response so I updated the method to only show value:

get_unrecognized_field_info()函数返回(value,variant)的元组。响应没有预期元组,所以我更新了方法只显示值:

    def test_api_key(self, request):
        return TestResponse(content=request.get_unrecognized_field_info('key')[0])