【问题标题】:Authenticate to google cloud function from local dev environment从本地开发环境对谷歌云功能进行身份验证
【发布时间】:2019-11-13 02:38:39
【问题描述】:

我的 Flask 应用程序在部署后进行身份验证,但显然我想在本地运行它以进行测试,但是它需要通过 google 进行身份验证。我在部署时使用以下内容进行身份验证,但是如何在本地创建 JWT 令牌?

def invoke_cloud_function(function_url, payload):
    # Make sure to replace variables with appropriate values
    receiving_function_url = function_url

    # Set up metadata server request
    metadata_server_token_url = 'http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience='

    token_request_url = metadata_server_token_url + receiving_function_url
    token_request_headers = {'Metadata-Flavor': 'Google'}

    # Fetch the token
    token_response = requests.get(token_request_url, headers=token_request_headers)
    jwt = token_response.content.decode("utf-8")

    # Provide the token in the request to the receiving function
    receiving_function_headers = {'Authorization': f'bearer {jwt}'}
    function_response = requests.get(receiving_function_url, params=payload, headers=receiving_function_headers)

    return function_response.json()

它试图指向metadata_server_token_url = 'http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience=,这将只在已部署的应用引擎本地。

我需要一种适合所有方式来验证云功能。我确实将环境变量设置为服务帐户密钥,但这没有帮助。

【问题讨论】:

    标签: google-app-engine google-cloud-functions


    【解决方案1】:

    根据官方文档Accessing Instance Metadata.

    元数据服务器可用于已部署的应用程序:正在运行 不支持本地在开发服务器上。您可以添加一个 环境检查您的代码以仅在 应用正在生产中运行。

     env =  os.environ.get('PROD', 'Specified environment variable is not set.')
    
     // The metadata server is only on a production system
     if env == "Production":
         //show metadata results
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-17
      • 1970-01-01
      • 1970-01-01
      • 2020-10-25
      • 2019-12-02
      • 1970-01-01
      相关资源
      最近更新 更多