【问题标题】:Is there any mechanism to use more than one GOOGLE_APPLICATION_CREDENTIALS in codebase?是否有任何机制可以在代码库中使用多个 GOOGLE_APPLICATION_CREDENTIALS?
【发布时间】:2019-12-28 15:43:48
【问题描述】:

我们在一个谷歌服务帐户中有多个项目,每个项目都有单独的GOOGLE_APPLICATION_CREDENTIALS json 文件。根据基于语言环境和projectID 的要求,我们必须使用相关的凭证 json 文件。

尝试通过环境变量加载,但只能接受一个文件路径,

设置环境变量

GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/[FILE_NAME].json"

如果请求来自具有该服务帐户的任何项目,则期望身份验证成功。

【问题讨论】:

    标签: dialogflow-es


    【解决方案1】:

    您需要直接从 json 文件设置凭据,而不是在环境变量中设置。

    from google.oauth2 import service_account
    SERVICE_ACCOUNT_FILE = "/home/user/Downloads/[FILE_NAME].json"
    credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE)
    project_id = "project_id"
    session_id = "session_id"
    session_client = dialogflow.SessionsClient(credentials=credentials)
    session = session_client.session_path(project_id, session_id)
    

    希望对你有帮助。

    【讨论】:

      【解决方案2】:
      // 1. read json by InputStream
          InputStream stream = context.getAssets().open("-----.json");
          GoogleCredentials credentials = GoogleCredentials.fromStream(stream);
          String projectId = ((ServiceAccountCredentials)credentials).getProjectId();
      
      // 2. build SessionSettings
          SessionsSettings.Builder settingsBuilder = SessionsSettings.newBuilder();
          SessionsSettings sessionsSettings = settingsBuilder.setCredentialsProvider(FixedCredentialsProvider.create(credentials)).build();
      
      // 3. create SessionsClient
          SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)
      

      【讨论】:

        【解决方案3】:

        您可以查看“-----.json”文件。

        有private_key和client_email。

        您可以在代码中使用它们。 这很好用。

        let config = {
            credentials: {
                private_key: private_key,
                client_email: client_email
            }
        };
        
        
        // Create a new session
        const sessionClient = new dialogflow.SessionsClient(config);
        const sessionPath = sessionClient.projectAgentSessionPath(
          projectId,
          sessionId
        );
        

        这会很有帮助。

        【讨论】:

          猜你喜欢
          • 2014-01-18
          • 1970-01-01
          • 2020-11-09
          • 2010-10-22
          • 2019-10-02
          • 1970-01-01
          • 2021-03-20
          • 2018-08-18
          • 2012-08-12
          相关资源
          最近更新 更多