Spring Boot 在IntelliJ IDEA中设置Google API的凭据

djmepvbi  于 2024-01-06  发布在  Spring
关注(0)|答案(1)|浏览(259)

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

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

字符串
我在json文件中有这个:

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


然后我运行这个服务:

  1. @Service
  2. public class GoogleGeminiService {
  3. public static void main(String[] args) throws Exception {
  4. String projectId = "MysticRiver";
  5. String location = "us-central1";
  6. String modelName = "gemini-pro-vision";
  7. String output = simpleQuestion(projectId, location, modelName);
  8. System.out.println(output);
  9. }
  10. }


但我犯了个错误

  1. ...context/6.1.1/spring-context-6.1.1.jar com.mysticriver.service.GoogleGeminiService
  2. 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.
  3. at com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentials(DefaultCredentialsProvider.java:127)

7gyucuyw

7gyucuyw1#

首先,服务帐户密钥文件是一个秘密,你不必分享它。这就是为什么我更新了你的问题,并编辑了内容(我建议你销毁密钥,它更安全)。
然后,你不需要钥匙,这不安全。这是一个秘密,你可以很容易地分享它(就像你一样)。
所以,我建议使用ADC与您自己的帐户(gcloud auth application-default login)或模拟使用服务帐户权限(添加参数--impersonate-service-account=<SA email>
因为您以安全的方式使用ADC,您的问题应该同时得到解决!

相关问题