[英]google app engine standard firebase authentication problem
I'm trying to write an API in Google App Engine Standard using Endpoints Framework.我正在尝试使用 Endpoints Framework 在 Google App Engine Standard 中编写 API。 I followed the tutorial at https://cloud.google.com/endpoints/docs/frameworks/python/get-started-frameworks-python .
我遵循了https://cloud.google.com/endpoints/docs/frameworks/python/get-started-frameworks-python 上的教程。 "Opened" methods works fine, but I have problems when I want to secure the API with Firebase authentication.
“打开”方法工作正常,但是当我想使用 Firebase 身份验证保护 API 时遇到问题。 Following the tutorial at https://cloud.google.com/endpoints/docs/frameworks/python/authenticating-users , I wrote the following code:
按照https://cloud.google.com/endpoints/docs/frameworks/python/authenticating-users 上的教程,我编写了以下代码:
import endpoints
from endpoints import message_types
from endpoints import messages
from endpoints import remote
class EchoRequest(messages.Message):
message = messages.StringField(1)
class EchoResponse(messages.Message):
"""A proto Message that contains a simple string field."""
message = messages.StringField(1)
ECHO_RESOURCE = endpoints.ResourceContainer(
EchoRequest,
n=messages.IntegerField(2, default=1))
@endpoints.api(
name='echo',
version='v1',
issuers={'firebase': endpoints.Issuer(
'https://securetoken.google.com/project_name',
'https://www.googleapis.com/service_accounts/v1/metadata/x509/securetoken@system.gserviceaccount.com')})
class EchoApi(remote.Service):
@endpoints.method(
# This method takes a ResourceContainer defined above.
ECHO_RESOURCE,
# This method returns an Echo message.
EchoResponse,
path='test_firebase',
http_method='POST',
name='test_firebase')
def test_firebase(self, request):
user = endpoints.get_current_user()
# If there's no user defined, the request was unauthenticated, so we
# raise 401 Unauthorized.
if not user:
raise endpoints.UnauthorizedException
output_message = ' '.join([request.message] * 3)
return EchoResponse(message=output_message)
When I try this method using the following curl:当我使用以下 curl 尝试此方法时:
TOKEN="token_string"
curl -i \
-H "Authorization: Bearer ${TOKEN}" \
-H "Content-Type: application/json" \
-X POST -d '{"message":"hello world"}' https://project_name.appspot.com/_ah/api/echo/v1/test_firebase
I have the following error:我有以下错误:
HTTP/2 401
content-type: application/json
x-cloud-trace-context: ffd02c47eb5885d6a93c31ac8aae26cc;o=1
date: Fri, 20 Dec 2019 17:55:57 GMT
server: Google Frontend
content-length: 150
alt-svc: quic=":443"; ma=2592000; v="46,43",h3-Q050=":443"; ma=2592000,h3-Q049=":443"; ma=2592000,h3-Q048=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000
{
"error": {
"code": 401,
"errors": [
{
"domain": "global",
"message": "",
"reason": "required"
}
],
"message": ""
}
}
Where is the error?错误在哪里?
(From alessandromercadante@'s comment, which does NOT work for me. Since it works for some and there is no answer yet, I copied it here to keep the thread going) (来自 alessandromercadante@ 的评论,这对我不起作用。由于它对某些人有效并且还没有答案,我将其复制到此处以保持线程继续进行)
In the signature of the method, the keyword audiences
must be passed:在方法的签名中,必须传递关键字
audiences
:
@endpoints.method(
ECHO_RESOURCE,
EchoResponse,
path='test_firebase',
audiences={ "firebase": [project_id]},
http_method='POST',
name='test_firebase')
def test_firebase(self, request):
user = endpoints.get_current_user()
if not user:
raise endpoints.UnauthorizedException
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.