| 32 | } |
| 33 | |
| 34 | async function exchangeCodeForTokens(code: string): Promise<{ refresh_token: string; access_token: string }> { |
| 35 | const response = await fetch('https://oauth2.googleapis.com/token', { |
| 36 | method: 'POST', |
| 37 | headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, |
| 38 | body: new URLSearchParams({ |
| 39 | client_id: CLIENT_ID!, |
| 40 | client_secret: CLIENT_SECRET!, |
| 41 | code, |
| 42 | grant_type: 'authorization_code', |
| 43 | redirect_uri: REDIRECT_URI, |
| 44 | }), |
| 45 | }); |
| 46 | |
| 47 | if (!response.ok) { |
| 48 | const error = await response.text(); |
| 49 | throw new Error(`Token exchange failed: ${error}`); |
| 50 | } |
| 51 | |
| 52 | return response.json(); |
| 53 | } |
| 54 | |
| 55 | async function main() { |
| 56 | console.log('\n🔐 Google OAuth Setup for Addie\n'); |