Spaces:
Sleeping
Sleeping
| /** | |
| * Get Gmail Refresh Token | |
| * | |
| * Run this script to get a refresh token for Gmail OAuth2. | |
| * | |
| * Usage: | |
| * 1. Update the CLIENT_ID and CLIENT_SECRET below with your credentials | |
| * 2. Run: node scripts/get-gmail-refresh-token.js | |
| * 3. Open the URL shown in browser | |
| * 4. Grant permissions | |
| * 5. Copy the refresh token from console | |
| */ | |
| const http = require('http'); | |
| const url = require('url'); | |
| const { google } = require('googleapis'); | |
| // ===== UPDATE THESE VALUES ===== | |
| const CLIENT_ID = '992906162498-09ptm95r4la42v08lisa3ka2a2ls4t8r.apps.googleusercontent.com'; | |
| const CLIENT_SECRET = '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'; | |
| const REDIRECT_URI = 'http://localhost:3000'; | |
| // ================================ | |
| const oauth2Client = new google.auth.OAuth2( | |
| CLIENT_ID, | |
| CLIENT_SECRET, | |
| REDIRECT_URI | |
| ); | |
| const SCOPES = ['https://www.googleapis.com/auth/gmail.send']; | |
| const authUrl = oauth2Client.generateAuthUrl({ | |
| access_type: 'offline', | |
| scope: SCOPES, | |
| prompt: 'consent', // Force consent to get a new refresh token | |
| }); | |
| console.log('\n========================================'); | |
| console.log('GMAIL OAuth2 REFRESH TOKEN GENERATOR'); | |
| console.log('========================================\n'); | |
| console.log('1. Open this URL in your browser:\n'); | |
| console.log(authUrl); | |
| console.log('\n2. Sign in with your Gmail account'); | |
| console.log('3. Grant permission to send emails'); | |
| console.log('4. The authorization code will be shown'); | |
| console.log('\n5. Copy the code and paste it here\n'); | |
| console.log('========================================\n'); | |
| const server = http.createServer(async (req, res) => { | |
| const parsedUrl = new url.URL(req.url, `http://${req.headers.host}`); | |
| const code = parsedUrl.searchParams.get('code'); | |
| if (code) { | |
| try { | |
| const { tokens } = await oauth2Client.getToken(code); | |
| console.log('\n✅ SUCCESS! Here are your tokens:\n'); | |
| console.log(`GMAIL_CLIENT_ID=${CLIENT_ID}`); | |
| console.log(`GMAIL_CLIENT_SECRET=${CLIENT_SECRET}`); | |
| console.log(`GMAIL_REFRESH_TOKEN=${tokens.refresh_token}`); | |
| console.log(`GMAIL_USER=scisnap000@gmail.com`); | |
| console.log(`GMAIL_ENABLED=true`); | |
| console.log('\n========================================\n'); | |
| console.log('Add these to your backend/.env file!\n'); | |
| res.writeHead(200, { 'Content-Type': 'text/html' }); | |
| res.end(` | |
| <html> | |
| <body style="font-family: Arial, sans-serif; padding: 40px; text-align: center;"> | |
| <h2 style="color: green;">✅ Authentication Successful!</h2> | |
| <p>Check your terminal/console for the tokens.</p> | |
| <p>You can close this window now.</p> | |
| </body> | |
| </html> | |
| `); | |
| } catch (error) { | |
| console.error('❌ Error:', error.message); | |
| res.writeHead(500, { 'Content-Type': 'text/html' }); | |
| res.end(`<h2>Error: ${error.message}</h2>`); | |
| } | |
| server.close(); | |
| } else { | |
| res.writeHead(400, { 'Content-Type': 'text/html' }); | |
| res.end('<h2>No authorization code received</h2>'); | |
| } | |
| }); | |
| server.listen(3000, () => { | |
| console.log('Server running at http://localhost:3000'); | |
| console.log('Waiting for authorization...\n'); | |
| }); |