Google App Engine标准Firebase身份验证问题

问题描述 投票:0回答:1

我正在尝试使用Endpoints Framework在Google App Engine标准中编写API。我按照https://cloud.google.com/endpoints/docs/frameworks/python/get-started-frameworks-python的指南进行操作。 “打开”方法可以正常工作,但是当我想使用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/[email protected]')})
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)

当我尝试使用以下卷曲进行此方法时:

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": ""
 }
}

错误在哪里?

google-app-engine google-oauth google-app-engine-python
1个回答
0
投票

(从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.UnauthorizedException


© www.soinside.com 2019 - 2024. All rights reserved.