This repository was archived by the owner on Dec 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgemini-auth-helper.sh
More file actions
executable file
·276 lines (237 loc) · 8.73 KB
/
Copy pathgemini-auth-helper.sh
File metadata and controls
executable file
·276 lines (237 loc) · 8.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/bin/bash
# Gemini CLI Authentication Helper
# Helps users set up either OAuth or API key authentication
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
print_header() {
echo -e "\n${BLUE}=== $1 ===${NC}\n"
}
print_success() {
echo -e "${GREEN}✓ $1${NC}"
}
print_error() {
echo -e "${RED}✗ $1${NC}"
}
print_info() {
echo -e "${BLUE}ℹ $1${NC}"
}
print_warning() {
echo -e "${YELLOW}⚠ $1${NC}"
}
# Check current authentication status
check_auth_status() {
print_header "Checking Current Authentication Status"
# Check for existing API keys
if [[ -n "$GOOGLE_API_KEY" ]]; then
print_success "GOOGLE_API_KEY is set in environment"
return 0
elif [[ -n "$GEMINI_API_KEY" ]]; then
print_success "GEMINI_API_KEY is set in environment"
return 0
elif [[ -f ".gemini/.env" ]] && grep -q "API_KEY" ".gemini/.env" 2>/dev/null; then
print_success "API key found in .gemini/.env file"
return 0
elif [[ -f ".env" ]] && grep -q "API_KEY" ".env" 2>/dev/null; then
print_success "API key found in .env file"
return 0
else
print_info "No API key configuration found"
fi
# Try to check OAuth status
if command -v gemini >/dev/null 2>&1; then
if gemini auth status 2>/dev/null | grep -q "authenticated"; then
print_success "OAuth authentication is active"
return 0
else
print_info "OAuth authentication not active"
fi
fi
return 1
}
# Setup API key authentication
setup_api_key() {
print_header "API Key Authentication Setup"
echo "API Key Authentication provides:"
echo "• Quick setup without browser"
echo "• 100 requests/day (free tier)"
echo "• No recurring authentication prompts"
echo "• Works in CI/CD environments"
echo ""
echo "Get your free API key from: https://aistudio.google.com/apikey"
echo ""
read -p "Do you have a Gemini API key? (y/n): " HAS_KEY
if [[ "$HAS_KEY" != "y" ]]; then
print_info "Please visit https://aistudio.google.com/apikey to generate a free API key"
echo "Then run this script again"
return 1
fi
echo ""
echo -n "Enter your Gemini API key: "
read -s API_KEY
echo ""
if [[ -z "$API_KEY" ]]; then
print_error "No API key provided"
return 1
fi
print_header "Choose Setup Method"
echo "1) Temporary (this session only)"
echo "2) Persistent - Add to shell profile (~/.bashrc or ~/.zshrc)"
echo "3) Project-specific - Create .gemini/.env file"
echo "4) Global .env - Create .env file in home directory"
echo ""
read -p "Select option (1-4): " OPTION
case $OPTION in
1)
export GOOGLE_API_KEY="$API_KEY"
print_success "API key set for this session"
print_info "Run 'export GOOGLE_API_KEY=\"$API_KEY\"' to set it again"
;;
2)
# Detect shell
if [[ -n "$ZSH_VERSION" ]]; then
SHELL_RC="$HOME/.zshrc"
elif [[ -n "$BASH_VERSION" ]]; then
SHELL_RC="$HOME/.bashrc"
else
SHELL_RC="$HOME/.$(basename $SHELL)rc"
fi
# Add to shell profile
echo "" >> "$SHELL_RC"
echo "# Gemini CLI API Key" >> "$SHELL_RC"
echo "export GOOGLE_API_KEY=\"$API_KEY\"" >> "$SHELL_RC"
# Source it
export GOOGLE_API_KEY="$API_KEY"
print_success "API key added to $SHELL_RC"
print_info "Run 'source $SHELL_RC' or restart your terminal"
;;
3)
mkdir -p .gemini
echo "GOOGLE_API_KEY=$API_KEY" > .gemini/.env
print_success "API key saved to .gemini/.env"
print_info "This will be used when running gemini from this directory"
;;
4)
echo "GOOGLE_API_KEY=$API_KEY" > "$HOME/.env"
print_success "API key saved to ~/.env"
print_info "This will be used globally"
;;
*)
print_error "Invalid option"
return 1
;;
esac
# Test the API key
print_header "Testing API Key"
export GOOGLE_API_KEY="$API_KEY"
if gemini "test connection" 2>&1 | grep -q "error"; then
print_error "API key test failed. Please check your key is valid"
return 1
else
print_success "API key is working!"
fi
}
# Setup OAuth authentication
setup_oauth() {
print_header "OAuth Authentication Setup"
echo "OAuth Authentication provides:"
echo "• 1,000 requests/day (10x more than API key)"
echo "• 60 requests/minute rate limit"
echo "• Secure token-based auth"
echo "• Best for development"
echo ""
print_warning "Note: OAuth may prompt for re-authentication periodically"
echo ""
read -p "Continue with OAuth setup? (y/n): " CONTINUE
if [[ "$CONTINUE" != "y" ]]; then
return 1
fi
print_info "Opening browser for Google authentication..."
gemini auth
if gemini auth status 2>/dev/null | grep -q "authenticated"; then
print_success "OAuth authentication successful!"
else
print_error "OAuth authentication failed or was cancelled"
return 1
fi
}
# Main menu
main() {
echo -e "${BLUE}"
echo "╔══════════════════════════════════════════════════╗"
echo "║ Gemini CLI Authentication Helper ║"
echo "╚══════════════════════════════════════════════════╝"
echo -e "${NC}"
# Check current status
if check_auth_status; then
echo ""
print_success "Authentication is already configured!"
echo ""
read -p "Do you want to reconfigure authentication? (y/n): " RECONFIG
if [[ "$RECONFIG" != "y" ]]; then
print_info "Keeping current authentication setup"
exit 0
fi
fi
print_header "Choose Authentication Method"
echo "1) API Key (Recommended for reliability)"
echo " • No browser needed"
echo " • 100 requests/day"
echo " • Never expires"
echo ""
echo "2) OAuth (Google Account)"
echo " • 1,000 requests/day"
echo " • Requires browser"
echo " • May need periodic re-auth"
echo ""
echo "3) View Authentication Comparison"
echo "4) Exit"
echo ""
read -p "Select option (1-4): " CHOICE
case $CHOICE in
1)
setup_api_key
;;
2)
setup_oauth
;;
3)
print_header "Authentication Method Comparison"
echo "┌─────────────────┬──────────────────┬─────────────────────┐"
echo "│ Feature │ API Key │ OAuth (Google) │"
echo "├─────────────────┼──────────────────┼─────────────────────┤"
echo "│ Daily Limit │ 100 requests │ 1,000 requests │"
echo "│ Rate Limit │ Limited │ 60/minute │"
echo "│ Setup │ Simple │ Requires browser │"
echo "│ Expiration │ Never │ Token refresh │"
echo "│ Best For │ CI/CD, Scripts │ Development │"
echo "│ MCP Support │ Yes │ Yes (preferred) │"
echo "└─────────────────┴──────────────────┴─────────────────────┘"
echo ""
read -p "Press Enter to return to menu..."
main
;;
4)
print_info "Exiting..."
exit 0
;;
*)
print_error "Invalid option"
main
;;
esac
# Show next steps
if [[ $? -eq 0 ]]; then
print_header "Next Steps"
echo "1. Run 'gemini-mcp-setup' to create Claude Code commands"
echo "2. Start using Gemini with Claude Code!"
echo ""
print_info "If you encounter issues, run this script again"
fi
}
# Run main function
main