Last active
January 15, 2026 17:57
-
-
Save shayelkin/bbb0418cb3ee09d89fab30820476d0ff to your computer and use it in GitHub Desktop.
Refresh Google ADC credentials (if needed)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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