|
| 1 | +# Case Study: Issue #100 - Google OAuth Scope Mismatch |
| 2 | + |
| 3 | +## Issue Reference |
| 4 | + |
| 5 | +- **Issue URL**: https://github.com/link-assistant/agent/issues/100 |
| 6 | +- **Title**: We should try all available auth credentials we have for Google |
| 7 | +- **Labels**: bug |
| 8 | +- **Date Reported**: December 23, 2025 |
| 9 | + |
| 10 | +## Timeline of Events |
| 11 | + |
| 12 | +1. **User installs agent v0.6.3**: `bun install -g @link-assistant/agent@latest` |
| 13 | +2. **User authenticates with Google OAuth**: Uses "Google AI Pro/Ultra (OAuth - Manual Code Entry)" method |
| 14 | +3. **Login appears successful**: Terminal shows "Login successful" |
| 15 | +4. **API request fails**: When using `echo "hi" | agent --model google/gemini-3-pro`, the request fails with error code 403 |
| 16 | + |
| 17 | +## Error Analysis |
| 18 | + |
| 19 | +### Error Response Details |
| 20 | + |
| 21 | +```json |
| 22 | +{ |
| 23 | + "error": { |
| 24 | + "code": 403, |
| 25 | + "message": "Request had insufficient authentication scopes.", |
| 26 | + "status": "PERMISSION_DENIED", |
| 27 | + "details": [ |
| 28 | + { |
| 29 | + "@type": "type.googleapis.com/google.rpc.ErrorInfo", |
| 30 | + "reason": "ACCESS_TOKEN_SCOPE_INSUFFICIENT", |
| 31 | + "domain": "googleapis.com", |
| 32 | + "metadata": { |
| 33 | + "method": "google.ai.generativelanguage.v1beta.GenerativeService.StreamGenerateContent", |
| 34 | + "service": "generativelanguage.googleapis.com" |
| 35 | + } |
| 36 | + } |
| 37 | + ] |
| 38 | + } |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +### WWW-Authenticate Header |
| 43 | + |
| 44 | +``` |
| 45 | +Bearer realm="https://accounts.google.com/", |
| 46 | +error="insufficient_scope", |
| 47 | +scope="https://www.googleapis.com/auth/generative-language |
| 48 | + https://www.googleapis.com/auth/generative-language.tuning |
| 49 | + https://www.googleapis.com/auth/generative-language.tuning.readonly |
| 50 | + https://www.googleapis.com/auth/generative-language.retriever |
| 51 | + https://www.googleapis.com/auth/generative-language.retriever.readonly" |
| 52 | +``` |
| 53 | + |
| 54 | +## Root Cause Analysis |
| 55 | + |
| 56 | +### Current Implementation (src/auth/plugins.ts) |
| 57 | + |
| 58 | +The Google OAuth plugin uses these scopes: |
| 59 | + |
| 60 | +```typescript |
| 61 | +const GOOGLE_OAUTH_SCOPES = [ |
| 62 | + 'https://www.googleapis.com/auth/cloud-platform', |
| 63 | + 'https://www.googleapis.com/auth/userinfo.email', |
| 64 | + 'https://www.googleapis.com/auth/userinfo.profile', |
| 65 | +]; |
| 66 | +``` |
| 67 | + |
| 68 | +### What Google API Requires |
| 69 | + |
| 70 | +The Google Generative Language API (used by gemini-3-pro) requires one of these scopes: |
| 71 | + |
| 72 | +- `https://www.googleapis.com/auth/generative-language` |
| 73 | +- `https://www.googleapis.com/auth/generative-language.tuning` |
| 74 | +- `https://www.googleapis.com/auth/generative-language.retriever` |
| 75 | + |
| 76 | +### The Mismatch |
| 77 | + |
| 78 | +**Problem**: The `cloud-platform` scope grants broad access to Google Cloud Platform resources, but it does NOT automatically grant access to the Generative Language API which uses a different scope hierarchy. |
| 79 | + |
| 80 | +**Gemini CLI Approach**: The official Gemini CLI (https://github.com/google-gemini/gemini-cli) also uses `cloud-platform` scope, suggesting that scope should work. However, the key difference may be in how the API is being called. |
| 81 | + |
| 82 | +## Key Findings |
| 83 | + |
| 84 | +### Authentication Methods for Google AI |
| 85 | + |
| 86 | +| Method | Required Scope | Works? | |
| 87 | +| --------------------------- | ----------------------------------- | ------------ | |
| 88 | +| API Key | None (uses `x-goog-api-key` header) | Yes | |
| 89 | +| OAuth (cloud-platform) | `cloud-platform` | Inconsistent | |
| 90 | +| OAuth (generative-language) | `generative-language.*` | Should work | |
| 91 | + |
| 92 | +### The Issue Title's Insight |
| 93 | + |
| 94 | +The issue title "We should try all available auth credentials we have for Google" suggests that: |
| 95 | + |
| 96 | +1. Users may have both OAuth tokens and API keys stored |
| 97 | +2. When OAuth fails due to scope issues, the system should fall back to API key authentication |
| 98 | +3. Currently, the system does not attempt alternative credentials |
| 99 | + |
| 100 | +## Current Auth Storage |
| 101 | + |
| 102 | +From `src/auth/index.ts`: |
| 103 | + |
| 104 | +```typescript |
| 105 | +export namespace Auth { |
| 106 | + export const Oauth = z.object({ |
| 107 | + type: z.literal('oauth'), |
| 108 | + refresh: z.string(), |
| 109 | + access: z.string(), |
| 110 | + expires: z.number(), |
| 111 | + enterpriseUrl: z.string().optional(), |
| 112 | + }); |
| 113 | + |
| 114 | + export const Api = z.object({ |
| 115 | + type: z.literal('api'), |
| 116 | + key: z.string(), |
| 117 | + }); |
| 118 | + |
| 119 | + // Auth is stored per-provider in auth.json |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +Auth credentials are stored by provider ID, meaning: |
| 124 | + |
| 125 | +- `google` -> OAuth credentials (with limited scope) |
| 126 | +- API keys from environment variables (`GOOGLE_GENERATIVE_AI_API_KEY` or `GEMINI_API_KEY`) |
| 127 | + |
| 128 | +## Proposed Solutions |
| 129 | + |
| 130 | +### Solution 1: Add Generative Language Scopes to OAuth |
| 131 | + |
| 132 | +Update `GOOGLE_OAUTH_SCOPES` in `src/auth/plugins.ts`: |
| 133 | + |
| 134 | +```typescript |
| 135 | +const GOOGLE_OAUTH_SCOPES = [ |
| 136 | + 'https://www.googleapis.com/auth/cloud-platform', |
| 137 | + 'https://www.googleapis.com/auth/userinfo.email', |
| 138 | + 'https://www.googleapis.com/auth/userinfo.profile', |
| 139 | + 'https://www.googleapis.com/auth/generative-language', |
| 140 | +]; |
| 141 | +``` |
| 142 | + |
| 143 | +**Pros**: Simple fix |
| 144 | +**Cons**: Users must re-authenticate; scope may not be available for all Google accounts |
| 145 | + |
| 146 | +### Solution 2: Credential Fallback Mechanism (Recommended) |
| 147 | + |
| 148 | +Implement a fallback strategy in `src/provider/provider.ts`: |
| 149 | + |
| 150 | +1. Try OAuth credentials first |
| 151 | +2. If OAuth fails with scope error (403), try API key |
| 152 | +3. If API key fails, report the original error |
| 153 | + |
| 154 | +This allows users who have both OAuth and API keys to seamlessly use whichever works. |
| 155 | + |
| 156 | +### Solution 3: Multiple Credential Storage |
| 157 | + |
| 158 | +Allow storing multiple credentials per provider: |
| 159 | + |
| 160 | +- OAuth tokens for subscription access |
| 161 | +- API keys for direct API access |
| 162 | + |
| 163 | +The system could try each credential in priority order until one succeeds. |
| 164 | + |
| 165 | +## Implementation Recommendation |
| 166 | + |
| 167 | +**Primary Fix**: Solution 2 - Credential Fallback Mechanism |
| 168 | + |
| 169 | +1. Modify the Google provider loader to attempt API key auth if OAuth fails |
| 170 | +2. Add error detection for scope-related 403 errors |
| 171 | +3. Log which credential method was used for transparency |
| 172 | +4. Maintain backward compatibility |
| 173 | + |
| 174 | +**Secondary Enhancement**: Solution 1 - Update OAuth Scopes |
| 175 | + |
| 176 | +After implementing fallback, also update the OAuth scopes to reduce the likelihood of scope errors for new authentications. |
| 177 | + |
| 178 | +## Implementation |
| 179 | + |
| 180 | +### Changes Made |
| 181 | + |
| 182 | +#### 1. Added All Required Generative Language Scopes (`src/auth/plugins.ts`) |
| 183 | + |
| 184 | +Updated `GOOGLE_OAUTH_SCOPES` to include all scopes mentioned in the API's WWW-Authenticate header: |
| 185 | + |
| 186 | +```typescript |
| 187 | +const GOOGLE_OAUTH_SCOPES = [ |
| 188 | + 'https://www.googleapis.com/auth/cloud-platform', |
| 189 | + 'https://www.googleapis.com/auth/userinfo.email', |
| 190 | + 'https://www.googleapis.com/auth/userinfo.profile', |
| 191 | + // Add generative-language scopes for Gemini API access |
| 192 | + 'https://www.googleapis.com/auth/generative-language', |
| 193 | + 'https://www.googleapis.com/auth/generative-language.tuning', |
| 194 | + 'https://www.googleapis.com/auth/generative-language.tuning.readonly', |
| 195 | + 'https://www.googleapis.com/auth/generative-language.retriever', |
| 196 | + 'https://www.googleapis.com/auth/generative-language.retriever.readonly', |
| 197 | +]; |
| 198 | +``` |
| 199 | + |
| 200 | +#### 2. Added Fallback to API Key on OAuth Scope Errors (`src/auth/plugins.ts`) |
| 201 | + |
| 202 | +In the Google OAuth loader, added logic to: |
| 203 | + |
| 204 | +1. Detect OAuth scope errors (HTTP 403 with `insufficient_scope` in `www-authenticate` header) |
| 205 | +2. Fall back to API key authentication if available (`GOOGLE_GENERATIVE_AI_API_KEY` or `GEMINI_API_KEY`) |
| 206 | +3. Log helpful warnings and hints for users |
| 207 | + |
| 208 | +```typescript |
| 209 | +const isScopeError = (response: Response): boolean => { |
| 210 | + if (response.status !== 403) return false; |
| 211 | + const wwwAuth = response.headers.get('www-authenticate') || ''; |
| 212 | + return ( |
| 213 | + wwwAuth.includes('insufficient_scope') || |
| 214 | + wwwAuth.includes('ACCESS_TOKEN_SCOPE_INSUFFICIENT') |
| 215 | + ); |
| 216 | +}; |
| 217 | + |
| 218 | +// In the fetch handler: |
| 219 | +if (isScopeError(oauthResponse)) { |
| 220 | + const fallbackApiKey = getFallbackApiKey(); |
| 221 | + if (fallbackApiKey) { |
| 222 | + log.warn('oauth scope error, falling back to api key authentication'); |
| 223 | + // Use API key instead of OAuth |
| 224 | + } |
| 225 | +} |
| 226 | +``` |
| 227 | + |
| 228 | +### How to Test |
| 229 | + |
| 230 | +1. **New users**: After updating, run `agent auth login` and select Google. The new OAuth flow will request all required scopes. |
| 231 | + |
| 232 | +2. **Existing users with scope errors**: If you have both OAuth and an API key, the system will automatically fall back to the API key. |
| 233 | + |
| 234 | +3. **Manual verification**: Check the logs for messages like: |
| 235 | + - `using google oauth credentials` - OAuth being used |
| 236 | + - `oauth scope error, falling back to api key authentication` - Fallback triggered |
| 237 | + |
| 238 | +## References |
| 239 | + |
| 240 | +- [Google OAuth Scopes Documentation](https://ai.google.dev/gemini-api/docs/oauth) |
| 241 | +- [Gemini CLI OAuth Implementation](https://github.com/google-gemini/gemini-cli/blob/main/packages/core/src/code_assist/oauth2.ts) |
| 242 | +- [Issue #51 - PaLM API 403 Insufficient Scopes](https://github.com/google/generative-ai-python/issues/51) |
| 243 | +- [Google Developer Forum - ACCESS_TOKEN_SCOPE_INSUFFICIENT](https://discuss.google.dev/t/googlegenerativeaierror-access-token-scope-insufficient/170693) |
0 commit comments