Python 2.7 对Google云端点API进行授权调用

Python 2.7 对Google云端点API进行授权调用,python-2.7,google-app-engine,jwt,google-cloud-endpoints,Python 2.7,Google App Engine,Jwt,Google Cloud Endpoints,我当前的设置 托管在谷歌应用程序引擎上的谷歌云端点。 谷歌回声教程() Python本地服务器向EchoAPI发出请求 echo教程已启动并正在运行。我可以在我的机器上使用python脚本调用开放端点和需要API密钥的端点。我无法使用Google ID令牌进行授权API调用。到目前为止,谷歌的例子都没有奏效 根据我的理解,工作流程应该是 使用密钥文件授权服务帐户生成JWT 使用JWT生成Google ID令牌 谷歌示例:(密钥文件) 代码失败了。函数get_id_token()返回res

我当前的设置

  • 托管在谷歌应用程序引擎上的谷歌云端点。
    • 谷歌回声教程()
  • Python本地服务器向EchoAPI发出请求
echo教程已启动并正在运行。我可以在我的机器上使用python脚本调用开放端点和需要API密钥的端点。我无法使用Google ID令牌进行授权API调用。到目前为止,谷歌的例子都没有奏效

根据我的理解,工作流程应该是

  • 使用密钥文件授权服务帐户生成JWT
  • 使用JWT生成Google ID令牌
  • 谷歌示例:(密钥文件) 代码失败了。函数get_id_token()返回res['id_token']失败,res中没有id_token


    有人把这个例子付诸实践了吗?有没有人举过从服务帐户使用Google ID令牌对端点API进行授权API调用的例子?

    主要问题是生成JWT,下面是适用于我的代码。我还没有找到一个更好的方法来做到这一点。如果您知道更好的方法,请在下面提交您的答案或添加评论。从JWT生成GoogleID令牌的代码完全来自Google文档here()get_ID_Token函数

    def generate_jwt(audience, json_keyfile, service_account_email):
    """Generates a signed JSON Web Token using a Google API Service Account.
        https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/endpoints/getting-started/clients/google-jwt-client.py
    """
    
    # Note: this sample shows how to manually create the JWT for the purposes
    # of showing how the authentication works, but you can use
    # google.auth.jwt.Credentials to automatically create the JWT.
    #   http://google-auth.readthedocs.io/en/latest/reference/google.auth.jwt.html#google.auth.jwt.Credentials
    
    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': 'https://www.googleapis.com/oauth2/v4/token',
        # target_audience must match 'audience' in the security configuration in your
        # openapi spec. It can be any string.
        'target_audience': audience,
        'iss': service_account_email
    }
    
    jwt = google.auth.jwt.encode(signer, payload)
    
    return jwt
    

    主要问题是生成JWT,下面是适用于我的代码。我还没有找到一个更好的方法来做到这一点。如果您知道更好的方法,请在下面提交您的答案或添加评论。从JWT生成GoogleID令牌的代码完全来自Google文档here()get_ID_Token函数

    def generate_jwt(audience, json_keyfile, service_account_email):
    """Generates a signed JSON Web Token using a Google API Service Account.
        https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/endpoints/getting-started/clients/google-jwt-client.py
    """
    
    # Note: this sample shows how to manually create the JWT for the purposes
    # of showing how the authentication works, but you can use
    # google.auth.jwt.Credentials to automatically create the JWT.
    #   http://google-auth.readthedocs.io/en/latest/reference/google.auth.jwt.html#google.auth.jwt.Credentials
    
    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': 'https://www.googleapis.com/oauth2/v4/token',
        # target_audience must match 'audience' in the security configuration in your
        # openapi spec. It can be any string.
        'target_audience': audience,
        'iss': service_account_email
    }
    
    jwt = google.auth.jwt.encode(signer, payload)
    
    return jwt
    

    当我使用此代码示例对进行请求时,我得到了一个未经授权的401。我似乎无法对此端点进行任何授权调用。您是否在日志中看到以下错误?错误:VerifiedHTTPSConnection“对象没有属性”\u tunnel\u host“如果有,请尝试按照此问题的答案进行操作。否-不允许使用客户端ID:。apps.googleusercontent.com,我认为这意味着客户端ID实际上无效。最后,我使用了你的优秀示例的一个变体来实现它!我们有针对nodejs的权限吗?当我使用此代码示例针对发出请求时,我得到了一个未经授权的401权限。我似乎无法对此端点进行任何授权调用。您是否在日志中看到以下错误?错误:VerifiedHTTPSConnection“对象没有属性”\u tunnel\u host“如果有,请尝试按照此问题的答案进行操作。否-不允许使用客户端ID:。apps.googleusercontent.com,我认为这意味着客户端ID实际上无效。最后,我使用了你的优秀示例的一个变体来实现它!我们有没有关于nodejs的建议?