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

djmepvbi  于 11个月前  发布在  Spring
关注(0)|答案(1)|浏览(198)

我在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)

7gyucuyw

7gyucuyw1#

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

相关问题