在IntelliJ IDEA配置Google API凭证时遇默认凭证未找到错误求助
解决Google默认凭证未找到问题
问题场景
我在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" }
随后运行Spring服务代码:
@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); } }
运行后出现错误:
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)
排查与解决步骤
1. 验证环境变量是否生效
在IntelliJ终端执行以下命令,确认环境变量已正确配置:
echo $GOOGLE_APPLICATION_CREDENTIALS
如果输出为空或路径错误,重新执行export命令,检查路径的引号、拼写是否正确。
2. 确保IntelliJ继承终端环境变量
- 若通过系统图标启动IntelliJ,终端设置的环境变量不会传递给IDE。需关闭IDE,从已设置环境变量的终端中重新启动(比如执行
open -a "IntelliJ IDEA.app")。 - 或直接在IDE运行配置中添加环境变量:
- 打开「Run/Debug Configurations」;
- 找到对应运行配置,切换到「Environment Variables」标签;
- 添加键值对:
GOOGLE_APPLICATION_CREDENTIALS=/Users/nunyito/IdeaProjects/mystic-river-api/src/main/resources/google-gemini/mysticriver.json; - 保存后重新运行。
3. 修复JSON文件语法错误
原JSON中project_id字段缺少闭合引号(原内容为"project_id": "mysticriver,),修正为:
"project_id": "mysticriver",
同时确认文件路径正确,当前用户拥有文件读取权限。
4. 代码中显式加载凭证(备选方案)
若环境变量方式仍不生效,可在代码中直接指定凭证路径,同时注意projectId要与JSON中的project_id完全一致(原代码中大小写不匹配):
import com.google.auth.oauth2.GoogleCredentials; import com.google.cloud.vertexai.VertexAI; import java.io.FileInputStream; public class GoogleGeminiService { public static void main(String[] args) throws Exception { String projectId = "mysticriver"; String location = "us-central1"; String modelName = "gemini-pro-vision"; // 显式加载凭证文件 GoogleCredentials credentials = GoogleCredentials.fromStream( new FileInputStream("/Users/nunyito/IdeaProjects/mystic-river-api/src/main/resources/google-gemini/mysticriver.json") ); try (VertexAI vertexAI = new VertexAI(projectId, location, credentials)) { String output = simpleQuestion(vertexAI, modelName); System.out.println(output); } } }
内容的提问来源于stack exchange,提问作者Nunyet Calçada
相关产品推荐
相关产品推荐

