loop-benchmarking

Controlled experiments across agentic coding configurations. Same task, one variable, what actually works.
git clone https://git.shiptheloop.com/loop-benchmarking.git
Log | Files | Refs | README

get-oauth-token.sh (1973B)


      1 #!/usr/bin/env bash
      2 # Extract OAuth token from Claude Code credentials for use with --bare mode.
      3 # Auto-refreshes the token if it's expired or about to expire.
      4 
      5 CREDS_FILE="${CLAUDE_CONFIG_DIR:-$HOME/.claude}/.credentials.json"
      6 
      7 if [[ ! -f "$CREDS_FILE" ]]; then
      8   echo "ERROR: No credentials file found at $CREDS_FILE" >&2
      9   exit 1
     10 fi
     11 
     12 # Check if token is expired or expires within 5 minutes
     13 expires_at=$(jq -r '.claudeAiOauth.expiresAt // 0' "$CREDS_FILE")
     14 now_ms=$(date +%s%3N 2>/dev/null || python3 -c "import time; print(int(time.time()*1000))")
     15 buffer_ms=300000  # 5 minutes
     16 
     17 if [[ $((expires_at - now_ms)) -lt $buffer_ms ]]; then
     18   # Token expired or about to expire - refresh it
     19   refresh_token=$(jq -r '.claudeAiOauth.refreshToken // empty' "$CREDS_FILE")
     20   if [[ -z "$refresh_token" ]]; then
     21     echo "ERROR: Token expired and no refresh token available" >&2
     22     exit 1
     23   fi
     24 
     25   # Call the OAuth refresh endpoint
     26   response=$(curl -s -X POST "https://console.anthropic.com/v1/oauth/token" \
     27     -H "Content-Type: application/x-www-form-urlencoded" \
     28     -d "grant_type=refresh_token" \
     29     -d "refresh_token=$refresh_token" \
     30     -d "client_id=claude-code")
     31 
     32   new_access=$(echo "$response" | jq -r '.access_token // empty')
     33   new_refresh=$(echo "$response" | jq -r '.refresh_token // empty')
     34   new_expires_in=$(echo "$response" | jq -r '.expires_in // 3600')
     35 
     36   if [[ -z "$new_access" ]]; then
     37     echo "ERROR: Token refresh failed: $response" >&2
     38     exit 1
     39   fi
     40 
     41   # Update credentials file
     42   new_expires_at=$(( $(date +%s) * 1000 + new_expires_in * 1000 ))
     43   tmp=$(jq \
     44     --arg at "$new_access" \
     45     --arg rt "${new_refresh:-$refresh_token}" \
     46     --argjson exp "$new_expires_at" \
     47     '.claudeAiOauth.accessToken = $at | .claudeAiOauth.refreshToken = $rt | .claudeAiOauth.expiresAt = $exp' \
     48     "$CREDS_FILE")
     49   echo "$tmp" > "$CREDS_FILE"
     50 
     51   echo "$new_access"
     52 else
     53   # Token is still valid
     54   jq -r '.claudeAiOauth.accessToken // empty' "$CREDS_FILE"
     55 fi

Impressum · Datenschutz