Skip to content

Instantly share code, notes, and snippets.

@shayelkin
Last active January 15, 2026 17:57
Show Gist options
  • Select an option

  • Save shayelkin/bbb0418cb3ee09d89fab30820476d0ff to your computer and use it in GitHub Desktop.

Select an option

Save shayelkin/bbb0418cb3ee09d89fab30820476d0ff to your computer and use it in GitHub Desktop.
Refresh Google ADC credentials (if needed)
#!/bin/bash
# `gcloud auth application-default print-access-token` can also serve as a check
# but takes a few seconds to run. POSTing directly to Google's oauth endpoint is
# much faster.
function check_adc {
ADC_FILE="$HOME/.config/gcloud/application_default_credentials.json"
[ ! -f "$ADC_FILE" ] && return 1
CLIENT_ID=$(jq -r .client_id "$ADC_FILE")
CLIENT_SECRET=$(jq -r .client_secret "$ADC_FILE")
REFRESH_TOKEN=$(jq -r .refresh_token "$ADC_FILE")
RESPONSE=$(curl -s -X POST https://oauth2.googleapis.com/token \
-d client_id="$CLIENT_ID" \
-d client_secret="$CLIENT_SECRET" \
-d refresh_token="$REFRESH_TOKEN" \
-d grant_type=refresh_token)
[[ "$RESPONSE" != *"access_token"* ]] && return 1
return 0
}
check_adc || gcloud auth application-default login 2>/dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment