如何对Google Cloud Endpoint进行身份验证?

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

我已经设置了一个简单的标准环境Google App Engine项目,该项目使用Cloud Endpoints,完成本教程中的步骤:

https://cloud.google.com/endpoints/docs/frameworks/python/get-started-frameworks-python

这很好用 - 我可以对回波端点进行卷曲调用并获得预期的结果。

但是,我无法成功调用经过身份验证的端点。我正在按照这里的步骤进行操作:https://cloud.google.com/endpoints/docs/frameworks/python/javascript-client,虽然我可以成功登录,但当我发送经过身份验证的请求时,我会收到401 Unauthorized HTTP响应。

从服务器上的日志我看到:

Client ID is not allowed: <my client id>.apps.googleusercontent.com (/base/data/home/apps/m~bfg-data-analytics/20190106t144214.415219868228932029/lib/endpoints/users_id_token.py:508)

到目前为止,我检查过:

  • Web应用程序正在使用正确版本的云端点配置。
  • 端点配置(x-google-audiences)中的客户端ID与javascript Web应用程序发布的客户端ID匹配。

有想法该怎么解决这个吗?

authentication google-cloud-platform google-cloud-endpoints restful-authentication
1个回答
0
投票

使用示例代码设置终点:https://cloud.google.com/endpoints/docs/frameworks/python/create_apihttps://cloud.google.com/endpoints/docs/frameworks/python/service-account-authentication

并修改python代码以生成令牌:https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/endpoints/getting-started/clients/service_to_service_google_id_token

我有它的工作。

这是服务器端点代码:

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={'serviceAccount': endpoints.Issuer(
    '[email protected]',
    'https://www.googleapis.com/robot/v1/metadata/x509/[email protected]')},
    audiences={'serviceAccount': ['[email protected]']})

class EchoApi(remote.Service):
    # Authenticated POST API
    # curl -H "Authorization: Bearer $token --request POST --header "Content-Type: applicationjson" --data '{"message":"echo"}' https://[email protected]/_ah/api/echo/v1/echo?n=5
    @endpoints.method(
        # This method takes a ResourceContainer defined above.
        ECHO_RESOURCE,
        # This method returns an Echo message.
        EchoResponse,
        path='echo',
        http_method='POST',
        name='echo')
    def echo(self, request):
        print "getting current user"
        user = endpoints.get_current_user()
        print user
        # if user == none return 401 unauthorized
        if not user:
            raise endpoints.UnauthorizedException

        # Create an output message including the user's email
        output_message = ' '.join([request.message] * request.n) + ' ' + user.email()
        return EchoResponse(message=output_message)

api = endpoints.api_server([EchoApi])

以及生成有效令牌的代码

    import base64
    import json
    import time
    import google

    import google.auth
    from google.auth import jwt

    def generate_token(audience, json_keyfile, client_id, service_account_email):
        signer = google.auth.crypt.RSASigner.from_service_account_file(json_keyfile)

        now = int(time.time())
        expires = now + 3600  # One hour in seconds

        payload = {
            'iat': now,
            'exp': expires,
            'aud' : audience,
            'iss': service_account_email,
            'sub': client_id,
            'email' : service_account_email
        }

        jwt = google.auth.jwt.encode(signer, payload)

        return jwt

token = generate_token(
    audience="[email protected]",              # must match x-google-audiences
    json_keyfile="./key-file-for-service-account.json",
    client_id="xxxxxxxxxxxxxxxxxxxxx",                              # client_id from key file
    service_account_email="[email protected]")

print token

使用curl进行经过身份验证的调用

export token=`python main.py` 
curl -H "Authorization: Bearer $token" --request POST --header "Content-Type: application/json" --data '{"message":"secure"}' https://MY-PROJECT.appspot.com/_ah/api/echo/v1/echo?n=5
© www.soinside.com 2019 - 2024. All rights reserved.