Search code examples
pythongoogle-cloud-endpoints

Google Cloud Endpoints Python Quickstart echo sample issue


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:

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.


Solution

  • There were three things that were creating the 503 error.

    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:

    @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.

    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:

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