Skip to main content
Glama
by mohalmah
oauth-setup-fixed.jsโ€ข6.81 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); } // Handle clear command if (process.argv.includes('--clear')) { tokenManager.clearTokens(); console.log('โœ… Tokens cleared successfully.'); 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); } } } 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}`); console.log(`๐Ÿ”’ File permissions: Owner read/write only`); console.log('\nโœ… Setup complete! Your OAuth tokens are now stored securely.'); console.log('๐Ÿ” Refresh tokens are stored in a secure OS-specific location'); console.log('๐Ÿš€ You can now use the MCP server and API tools'); console.log('\n๐Ÿงช Test your setup with:'); console.log(' node test-token-management.js'); } catch (saveError) { console.error('\nโŒ Failed to save tokens:', saveError.message); console.log('๐Ÿ”ง Please check file permissions and try again'); 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 setupOAuth().catch((error) => { console.error('๐Ÿ’ฅ Unexpected error:', error); process.exit(1); });

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