Skip to main content
Glama
by mohalmah
oauth-setup-broken.js•10.5 kB
#!/usr/bin/env node /** * OAuth Setup Script for Google Apps Script API * This script helps you obtain and securely store OAuth tokens */ import 'dotenv/config'; import { manualOAuthFlow } from '../lib/oauth-helper.js'; import { TokenManager } from '../lib/tokenManager.js'; import { readFileSync } from 'fs'; console.log('šŸ” Google Apps Script API OAuth Setup'); console.log('=====================================\n'); async function setupOAuth() { console.log('šŸ“‹ This script will help you set up OAuth authentication for Google Apps Script API.'); console.log('šŸ“ You need to have your CLIENT_ID and CLIENT_SECRET configured in .env file.\n'); const tokenManager = new TokenManager(); // Handle info command if (process.argv.includes('--info')) { const tokenInfo = tokenManager.getTokenInfo(); console.log('šŸ” Token Information:'); console.log('=====================\n'); if (tokenInfo.hasTokens) { console.log('āœ… Tokens found'); console.log(`šŸ“ Location: ${tokenInfo.location}`); console.log(`šŸ’¾ Saved at: ${tokenInfo.savedAt}`); console.log(`ā° Expires at: ${tokenInfo.expiresAt}`); console.log(`šŸ“Š Status: ${tokenInfo.status}`); console.log(`šŸ” Scope: ${tokenInfo.scope || 'Not specified'}`); } else { console.log('āŒ No tokens found'); console.log(`šŸ“ Expected location: ${tokenInfo.location}`); console.log('\nšŸ’” Run "node oauth-setup.js" to set up OAuth tokens'); } process.exit(0); } // Check if tokens already exist const tokenInfo = tokenManager.getTokenInfo(); if (tokenInfo.hasTokens) { console.log('šŸ” Found existing tokens:'); console.log(` šŸ“ Location: ${tokenInfo.location}`); console.log(` šŸ’¾ Saved at: ${tokenInfo.savedAt}`); console.log(` ā° Expires at: ${tokenInfo.expiresAt}`); console.log(` šŸ“Š Status: ${tokenInfo.status}`); console.log(` šŸ” Scope: ${tokenInfo.scope || 'Not specified'}\n`); if (!tokenInfo.isExpired) { console.log('āœ… You already have valid tokens stored.'); console.log('šŸ’” To get new tokens, run: node oauth-setup.js --force'); console.log('šŸ—‘ļø To clear existing tokens, run: node oauth-setup.js --clear\n'); if (!process.argv.includes('--force')) { process.exit(0); } } } // Handle clear command if (process.argv.includes('--clear')) { tokenManager.clearTokens(); console.log('āœ… Tokens cleared successfully.'); process.exit(0); } try { // Check if .env file exists and has required credentials const envPath = '.env'; let envContent = ''; try { envContent = readFileSync(envPath, 'utf8'); console.log('āœ… Found .env file'); } catch (error) { console.error('āŒ No .env file found. Please create one first with your CLIENT_ID and CLIENT_SECRET.'); console.log('\nšŸ“ Example .env file content:'); console.log('GOOGLE_APP_SCRIPT_API_CLIENT_ID=your_client_id_here'); console.log('GOOGLE_APP_SCRIPT_API_CLIENT_SECRET=your_client_secret_here'); console.log('\nšŸ“– Note: Refresh token is now stored securely and not needed in .env file'); process.exit(1); } // Check for required credentials const hasClientId = envContent.includes('GOOGLE_APP_SCRIPT_API_CLIENT_ID=') && !envContent.includes('GOOGLE_APP_SCRIPT_API_CLIENT_ID=your_client_id_here'); const hasClientSecret = envContent.includes('GOOGLE_APP_SCRIPT_API_CLIENT_SECRET=') && !envContent.includes('GOOGLE_APP_SCRIPT_API_CLIENT_SECRET=your_client_secret_here'); if (!hasClientId || !hasClientSecret) { console.error('āŒ Missing CLIENT_ID or CLIENT_SECRET in .env file.'); console.log('\nšŸ”§ Please update your .env file with valid credentials:'); console.log(' - GOOGLE_APP_SCRIPT_API_CLIENT_ID=your_actual_client_id'); console.log(' - GOOGLE_APP_SCRIPT_API_CLIENT_SECRET=your_actual_client_secret'); console.log('\nšŸ“– See OAUTH_SETUP.md for instructions on obtaining these credentials.'); process.exit(1); } console.log('āœ… Found required credentials in .env file'); console.log('\nšŸš€ Starting OAuth flow...'); console.log('šŸ“± Your browser will open automatically'); console.log('šŸ” Please authorize the application when prompted'); console.log('ā³ Waiting for authorization...\n'); // Start OAuth flow const tokens = await manualOAuthFlow(); if (tokens.refresh_token) { console.log('\nšŸŽ‰ OAuth setup successful!'); console.log('šŸ”‘ Access token obtained:', tokens.access_token ? 'āœ…' : 'āŒ'); console.log('šŸ”„ Refresh token obtained:', tokens.refresh_token ? 'āœ…' : 'āŒ'); // Save tokens securely using TokenManager try { tokenManager.saveTokens(tokens); console.log('šŸ’¾ Tokens saved securely'); const tokenInfo = tokenManager.getTokenInfo(); console.log(`šŸ“ Token location: ${tokenInfo.location}`); } catch (error) { console.error('āŒ Failed to save tokens:', error.message); console.log('\nšŸ“ Please run the setup again or contact support.'); process.exit(1); } console.log('\nšŸ“‹ Setup Summary:'); console.log(' āœ… OAuth flow completed'); console.log(' āœ… Access token obtained'); console.log(' āœ… Refresh token obtained'); console.log(' āœ… Tokens stored securely'); console.log('\nšŸ” Security Notes:'); console.log(' šŸ”’ Refresh token is stored with restricted file permissions'); console.log(' ā° Access token will be refreshed automatically'); console.log(' 🚫 No sensitive tokens are stored in .env file'); console.log('\nšŸŽÆ Next Steps:'); console.log(' 1. Test the OAuth setup: npm run test-oauth'); console.log(' 2. Configure your MCP client (Claude Desktop, VS Code, etc.)'); console.log(' 3. Use your MCP tools with confidence!'); } else { console.error('\nāŒ OAuth setup failed: No refresh token received'); console.log('šŸ”§ This might happen if:'); console.log(' - Your OAuth app is not configured correctly'); console.log(' - You denied the authorization request'); console.log(' - There was a network error during the process'); console.log('\nšŸ“– Please check the OAUTH_SETUP.md guide and try again.'); process.exit(1); } } catch (error) { console.error('\nāŒ OAuth setup failed:', error.message); if (error.message.includes('EADDRINUSE')) { console.log('\nšŸ”§ Port already in use. Please:'); console.log(' 1. Close any other applications using port 3001'); console.log(' 2. Wait a moment and try again'); } else if (error.message.includes('CLIENT_ID') || error.message.includes('CLIENT_SECRET')) { console.log('\nšŸ”§ OAuth credential issue. Please:'); console.log(' 1. Check your .env file has correct credentials'); console.log(' 2. Verify credentials in Google Cloud Console'); console.log(' 3. Make sure OAuth consent screen is configured'); } else { console.log('\nšŸ”§ Please check:'); console.log(' 1. Your internet connection'); console.log(' 2. Google Cloud Console OAuth configuration'); console.log(' 3. That you authorized the application in the browser'); } console.log('\nšŸ“– For detailed setup instructions, see OAUTH_SETUP.md'); process.exit(1); } } // Handle command line arguments if (process.argv.includes('--help') || process.argv.includes('-h')) { console.log('šŸ“– Google Apps Script OAuth Setup'); console.log('\nUsage:'); console.log(' node oauth-setup.js # Run OAuth setup'); console.log(' node oauth-setup.js --force # Force new OAuth setup (overwrite existing tokens)'); console.log(' node oauth-setup.js --clear # Clear stored tokens'); console.log(' node oauth-setup.js --info # Show token information'); console.log(' node oauth-setup.js --help # Show this help'); process.exit(0); } if (process.argv.includes('--info')) { const tokenManager = new TokenManager(); const tokenInfo = tokenManager.getTokenInfo(); console.log('šŸ” Token Information:'); console.log('=====================\n'); if (tokenInfo.hasTokens) { console.log('āœ… Tokens found'); console.log(`šŸ“ Location: ${tokenInfo.location}`); console.log(`šŸ’¾ Saved at: ${tokenInfo.savedAt}`); console.log(`ā° Expires at: ${tokenInfo.expiresAt}`); console.log(`šŸ“Š Status: ${tokenInfo.status}`); console.log(`šŸ” Scope: ${tokenInfo.scope || 'Not specified'}`); } else { console.log('āŒ No tokens found'); console.log(`šŸ“ Expected location: ${tokenInfo.location}`); console.log('\nšŸ’” Run "node oauth-setup.js" to set up OAuth tokens'); } process.exit(0); } // Run setup setupOAuth().catch((error) => { console.error('šŸ’„ Unexpected error:', error); process.exit(1); }); } else { console.log('\nāš ļø OAuth completed but no refresh token received.'); console.log('šŸ”„ You may need to revoke and re-authorize the application.'); console.log('šŸ“– Check the Google Cloud Console for your OAuth settings.'); } } catch (error) { console.error('\nāŒ OAuth setup failed:', error.message); console.log('\nšŸ”§ Troubleshooting:'); console.log(' 1. Check your internet connection'); console.log(' 2. Verify your CLIENT_ID and CLIENT_SECRET are correct'); console.log(' 3. Ensure the redirect URI is registered in Google Cloud Console'); console.log(' 4. Make sure Google Apps Script API is enabled'); console.log(' 5. Try revoking and re-creating your OAuth credentials'); console.log('\nšŸ“– For detailed setup instructions, see OAUTH_SETUP.md'); process.exit(1); } } // Run setup if this script is executed directly console.log('šŸ” Debug: process.argv[1]:', process.argv[1]); console.log('šŸ” Debug: endsWith check:', process.argv[1] && process.argv[1].endsWith('oauth-setup.js')); if (process.argv[1] && process.argv[1].endsWith('oauth-setup.js')) { console.log('šŸš€ Starting OAuth setup...'); setupOAuth(); } else { console.log('āŒ Script not executed directly, skipping setup'); }

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/mohalmah/google-appscript-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server