在 IntelliJ IDEA 中设置 Google API 的凭据

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

我在 IntelliJ IDEA 中打开一个终端并运行以下命令:

export GOOGLE_APPLICATION_CREDENTIALS="/Users/nunyito/IdeaProjects/mystic-river-api/src/main/resources/google-gemini/mysticriver.json"

我在 json 文件中有这个:

{
  "type": "service_account",
  "project_id": "mysticriver,
  "private_key_id": "c81d561c1z65159b925ddd7a00c895358855b855c",
  "private_key": "-----BEGIN PRIVATE KEY-----\nREDACTED\n-----END PRIVATE KEY-----\n",
  "client_email": "service-account-for-mystic-riv@mysticriver.iam.gserviceaccount.com",
  "client_id": "117167372461671003080",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/service-account-for-mystic-riv%40mysticriver.iam.gserviceaccount.com",
  "universe_domain": "googleapis.com"
}

然后我运行此服务:

@Service
public class GoogleGeminiService {

    public static void main(String[] args) throws Exception {
       
        String projectId = "MysticRiver";
        String location = "us-central1";
        String modelName = "gemini-pro-vision";

        String output = simpleQuestion(projectId, location, modelName);
        System.out.println(output);
    }
}

但是我有这个错误:

...context/6.1.1/spring-context-6.1.1.jar com.mysticriver.service.GoogleGeminiService
Exception in thread "main" java.io.IOException: Your default credentials were not found. To set up Application Default Credentials for your environment, see https://cloud.google.com/docs/authentication/external/set-up-adc.
    at com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentials(DefaultCredentialsProvider.java:127)
java spring-boot google-cloud-platform google-api google-oauth
1个回答
0
投票

首先,服务帐户密钥文件是一个秘密,您不必共享它。这就是为什么我更新了你的问题并编辑了内容(我建议你销毁密钥,这样更安全)。

那么,你就不需要钥匙了,不安全。这是一个秘密,你可以很容易地分享它(就像你所做的那样)。

所以,我建议使用自己的帐户使用ADC(

gcloud auth application-default login
)或模拟使用服务帐户权限(添加参数
--impersonate-service-account=<SA email>

因为您以安全的方式使用ADC,您的问题应该同时得到解决!

© www.soinside.com 2019 - 2024. All rights reserved.