【问题标题】:Set GOOGLE_APPLICATION_CREDENTIALS environment variable in VSCode debugger在 VSCode 调试器中设置 GOOGLE_APPLICATION_CREDENTIALS 环境变量
【发布时间】:2020-02-24 12:53:06
【问题描述】:

我正在我的 Mac 上使用 VSCode 在我的 Django 项目上测试 Google Cloud Pub/Sub,但在调试测试期间设置 GCP 环境变量时遇到了一些问题,特别是 GOOGLE_APPLICATION_CREDENTIALS 环境变量。

我可以设置GOOGLE_APPLICATION_CREDENTIALS 以在shell 中使用export GOOGLE_APPLICATION_CREDENTIALS="path/to/json_credential_file.json" 在本地运行服务器和/或测试,但是由于VSCode 调试器在单独的shell 中运行,因此在调试测试时未设置env 变量。我尝试将相同的值添加到launch.json 中的env 键:

"env": {
    "GOOGLE_APPLICATION_CREDENTIALS": "path/to/json_credential_file.json"
}

但它仍然抱怨没有设置 GOOGLE_APPLICATION_CREDENTIALS:

oauth2client.client.ApplicationDefaultCredentialsError: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.

有什么想法吗?

【问题讨论】:

    标签: django visual-studio-code google-cloud-platform environment-variables vscode-debugger


    【解决方案1】:

    在 Powershell 中,运行

    [Environment]::SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "path\serviceAccountTasks.privateKey.json", "User")

    这将为所有进程设置环境变量

    【讨论】:

      【解决方案2】:

      这是一个例子

      "env": {
              "GOOGLE_APPLICATION_CREDENTIALS": "./secret.do.not.commit/serviceAccountTasks.privateKey.json"
            }
      

      这里是参考https://code.visualstudio.com/docs/editor/debugging#_launch-configurations

      【讨论】:

        【解决方案3】:

        您可以使用启动配置并在特定于您的编程语言的配置中设置“env”变量。

        https://code.visualstudio.com/docs/editor/debugging#_launch-configurations

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-01-24
          • 2022-08-19
          • 1970-01-01
          • 2018-12-02
          • 1970-01-01
          • 2021-01-15
          相关资源
          最近更新 更多