我正在尝试使用终结点框架在Google App Engine标准中编写API。我遵循了https://cloud.google.com/endpoints/docs/frameworks/python/get-started-frameworks-python的教程。"Opened“方法可以很好地工作,但当我想用Firebase身份验证保护API时,我就遇到了问题。按照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)当我使用下面的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我有以下错误:
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": ""
}
}错误在哪里?
发布于 2020-04-20 03:27:16
(来自alessandromercadante@的评论,这个评论对我不起作用。因为它对一些人有效,而且还没有答案,所以我把它复制到这里来保持帖子的运行)
在方法的签名中,必须传递关键字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.UnauthorizedExceptionhttps://stackoverflow.com/questions/59429594
复制相似问题