You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

设置GOOGLE_APPLICATION_CREDENTIALS环境变量后下一步及报错解决求助

Fixing "Application Default Credentials are not available" Error (Even After Setting Environment Variable)

Hey there! Let's work through this frustrating error together. You mentioned you've set the GOOGLE_APPLICATION_CREDENTIALS environment variable, but the error still pops up—here's how to diagnose and fix it step by step:

Step 1: Verify the Environment Variable is Correctly Set

First, double-check that the variable is actually pointing to the right file in your current session:

  • Windows (Command Prompt): Run echo %GOOGLE_APPLICATION_CREDENTIALS%
  • macOS/Linux/Terminal: Run echo $GOOGLE_APPLICATION_CREDENTIALS

Make sure:

  • The output is an absolute path (not a relative path like ./key.json)
  • The path is spelled correctly (watch for typos, especially case sensitivity on macOS/Linux)
  • The file exists at that location (navigate to the path to confirm it’s there)

Step 2: Validate the Credentials File

Open the JSON key file you downloaded from Google Cloud Console and confirm it has critical fields like client_email and private_key. If the file is corrupted, truncated, or not a valid service account key, it won’t work—re-download the key from the Cloud Console if needed.

Step 3: Ensure the Variable is Active in Your Program's Session

Environment variables often require a session restart to take effect:

  • If you set the variable in your terminal, restart the terminal before running your program
  • If you’re using an IDE (like IntelliJ, VS Code), close and reopen the IDE to load the updated variables
  • On Linux/macOS, if you added the variable to ~/.bashrc or ~/.zshrc, run source ~/.bashrc (or your shell’s config file) to apply changes immediately

Step 4: Bypass Environment Variables (Manual Credential Loading)

If the environment variable approach still fails, load the credentials directly in your code to rule out environment issues. For Java (since your error trace mentions com.google.auth.oauth2.Def...), use this snippet:

import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import java.io.FileInputStream;
import java.io.IOException;

public class CredentialExample {
    public static void main(String[] args) throws IOException {
        GoogleCredentials credentials = GoogleCredentials.fromStream(
            new FileInputStream("/absolute/path/to/your/service-account-key.json")
        );
        Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();
        // Use the storage client as needed
    }
}

Bonus: Check File Permissions

On Linux/macOS, ensure your program has read access to the key file. Run chmod 600 /path/to/key.json to restrict access to only your user (this is also a security best practice).

内容的提问来源于stack exchange,提问作者Umang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:00:50