I struggled to get multiple APIs working correctly for days.
After I deployed the service using the steps below, although both api require api key, the problems are:
- I can call one of them without providing api key while the other does require an api key.
- Only one of the API are showing up in developer portal. Sometimes AuthedGreetingApi, sometimes GreetingApi.
Is this a bug for cloud endpoints?
python lib/endpoints/endpointscfg.py get_openapi_spec main.GreetingApi main.AuthedGreetingApi --hostname inner-cinema-211407.appspot.com --x-google-api-name
this generates greetingv1openapi.json and authedgreetingv1openapi.json
gcloud endpoints services deploy greetingv1openapi.json authedgreetingv1openapi.json result: Service Configuration [2019-01-07r9] uploaded for service [inner-cinema-211407.appspot.com]
update the app.yaml as following:
env_variables:
# The following values are to be replaced by information from the output of
# 'gcloud endpoints services deploy swagger.json' command.
ENDPOINTS_SERVICE_NAME: inner-cinema-211407.appspot.com
ENDPOINTS_SERVICE_VERSION: 2019-01-07r9
deploy to GAE
gcloud app deploy --project=inner-cinema-211407
Source code:
# [START messages]classGreeting(messages.Message):
"""Greeting that stores a message."""message=messages.StringField(1)
classGreetingCollection(messages.Message):
"""Collection of Greetings."""items=messages.MessageField(Greeting, 1, repeated=True)
STORED_GREETINGS=GreetingCollection(items=[
Greeting(message='hello world!'),
Greeting(message='goodbye world!'),
])
# [END messages]# [START greeting_api]@endpoints.api(name='greeting', version='v1', api_key_required=True)classGreetingApi(remote.Service):
@endpoints.method(# This method does not take a request message.message_types.VoidMessage,# This method returns a GreetingCollection message.GreetingCollection,path='greetings',http_method='GET',name='greetings.list')deflist_greetings(self, unused_request):
returnSTORED_GREETINGS# ResourceContainers are used to encapsuate a request body and url# parameters. This one is used to represent the Greeting ID for the# greeting_get method.GET_RESOURCE=endpoints.ResourceContainer(
# The request body should be empty.message_types.VoidMessage,
# Accept one url parameter: and integer named 'id'id=messages.IntegerField(1, variant=messages.Variant.INT32))
@endpoints.method(# Use the ResourceContainer defined above to accept an empty body# but an ID in the query string.GET_RESOURCE,# This method returns a Greeting message.Greeting,# The path defines the source of the URL parameter 'id'. If not# specified here, it would need to be in the query string.path='greetings/{id}',http_method='GET',name='greetings.get')defget_greeting(self, request):
try:
# request.id is used to access the URL parameter.returnSTORED_GREETINGS.items[request.id]
except (IndexError, TypeError):
raiseendpoints.NotFoundException(
'Greeting {} not found'.format(request.id))
# [END greeting_api]# [START multiply]# This ResourceContainer is similar to the one used for get_greeting, but# this one also contains a request body in the form of a Greeting message.MULTIPLY_RESOURCE=endpoints.ResourceContainer(
Greeting,
times=messages.IntegerField(2, variant=messages.Variant.INT32,
required=True))
@endpoints.method(# This method accepts a request body containing a Greeting message# and a URL parameter specifying how many times to multiply the# message.MULTIPLY_RESOURCE,# This method returns a Greeting message.Greeting,path='greetings/multiply/{times}',http_method='POST',name='greetings.multiply')defmultiply_greeting(self, request):
returnGreeting(message=request.message*request.times)
# [END multiply]# [START auth_config]WEB_CLIENT_ID='replace this with your web client application ID'ANDROID_CLIENT_ID='replace this with your Android client ID'IOS_CLIENT_ID='replace this with your iOS client ID'ANDROID_AUDIENCE=WEB_CLIENT_IDALLOWED_CLIENT_IDS= [
WEB_CLIENT_ID, ANDROID_CLIENT_ID, IOS_CLIENT_ID,
endpoints.API_EXPLORER_CLIENT_ID]
# [END auth_config]# [START authed_greeting_api]@endpoints.api(name='authedgreeting',version='v1',# Only allowed configured Client IDs to access this API.allowed_client_ids=ALLOWED_CLIENT_IDS,# Only allow auth tokens with the given audience to access this API.audiences=[ANDROID_AUDIENCE],api_key_required=True,# Require auth tokens to have the following scopes to access this API.scopes=[endpoints.EMAIL_SCOPE])classAuthedGreetingApi(remote.Service):
@endpoints.method(message_types.VoidMessage,Greeting,path='greet',http_method='POST',name='greet')defgreet(self, request):
user=endpoints.get_current_user()
user_name=user.email() ifuserelse'Anonymous'returnGreeting(message='Hello, {}'.format(user_name))
# [END authed_greeting_api]# [START api_server]api=endpoints.api_server([GreetingApi, AuthedGreetingApi])
# [END api_server]
I struggled to get multiple APIs working correctly for days.
After I deployed the service using the steps below, although both api require api key, the problems are:
Is this a bug for cloud endpoints?
this generates
greetingv1openapi.jsonandauthedgreetingv1openapi.jsonupdate the app.yaml as following:
deploy to GAE
Source code: