test-oauth.jsâĸ6.17 kB
#!/usr/bin/env node
/**
* Test script to verify OAuth authentication setup
*/
import 'dotenv/config';
import { getOAuthAccessToken, getAuthHeaders } from './lib/oauth-helper.js';
async function testOAuthAuthentication() {
console.log('đ Testing OAuth Authentication for Google Apps Script API...\n');
console.log('đ Test started at:', new Date().toISOString());
console.log('đ Working directory:', process.cwd());
console.log('đ§ Node.js version:', process.version);
console.log('');
try {
// Test 1: Get access token
console.log('đ Step 1: Getting OAuth access token...');
console.log('âŗ Attempting to retrieve access token from OAuth helper...');
const startTime = Date.now();
const accessToken = await getOAuthAccessToken();
const duration = Date.now() - startTime;
console.log('â
Successfully obtained access token:', accessToken.substring(0, 20) + '...');
console.log('âąī¸ Token retrieval took:', duration + 'ms');
console.log('đ Full token length:', accessToken.length, 'characters');
console.log('');
// Test 2: Get auth headers
console.log('đ Step 2: Creating authorization headers...');
console.log('âŗ Building authorization headers for API requests...');
const headerStartTime = Date.now();
const headers = await getAuthHeaders();
const headerDuration = Date.now() - headerStartTime;
console.log('â
Successfully created auth headers:', JSON.stringify(headers, null, 2));
console.log('âąī¸ Header creation took:', headerDuration + 'ms');
console.log('đ Header keys count:', Object.keys(headers).length);
console.log('');
// Test 3: Test API call (optional - requires valid script ID)
console.log('đ Step 3: Testing API connectivity...');
console.log('âšī¸ To test a full API call, you would need a valid script ID.');
console.log('âšī¸ You can test with the script_processes_list tool in your MCP client.\n');
const totalDuration = Date.now() - startTime;
console.log('đ OAuth authentication test completed successfully!');
console.log('â
Your OAuth setup is working correctly.');
console.log('âąī¸ Total test duration:', totalDuration + 'ms');
console.log('đ Test completed at:', new Date().toISOString());
console.log('');
console.log('đ Next steps:');
console.log('1. Test one of the tools in your MCP client (Claude Desktop, Postman, etc.)');
console.log('2. Use a valid Google Apps Script project ID when calling the tools');
console.log('3. Ensure your OAuth token has the required scopes for the operations you want to perform');
} catch (error) {
console.error('â OAuth authentication failed!');
console.error('đ Error occurred at:', new Date().toISOString());
console.error('');
// Detailed error logging
console.error('đ Error Details:');
console.error(' đ Message:', error.message);
console.error(' đˇī¸ Name:', error.name);
console.error(' đ Stack trace:');
if (error.stack) {
console.error(error.stack.split('\n').map(line => ' ' + line).join('\n'));
} else {
console.error(' (No stack trace available)');
}
console.error('');
// Additional error information
if (error.code) {
console.error(' đĸ Error code:', error.code);
}
if (error.status) {
console.error(' đ HTTP status:', error.status);
}
if (error.statusText) {
console.error(' đ Status text:', error.statusText);
}
if (error.response) {
console.error(' đŦ Response data:', JSON.stringify(error.response, null, 2));
}
console.error('');
// Environment check
console.log('đ Environment Check:');
console.log(' đ Current directory:', process.cwd());
console.log(' đ§ Node.js version:', process.version);
console.log(' đž Platform:', process.platform);
console.log(' đī¸ Architecture:', process.arch);
// Check for .env file
try {
const fs = await import('fs');
const envPath = '.env';
const envExists = fs.existsSync(envPath);
console.log(' đ .env file exists:', envExists);
if (envExists) {
const envContent = fs.readFileSync(envPath, 'utf8');
const envLines = envContent.split('\n').filter(line => line.trim() && !line.startsWith('#'));
console.log(' đ .env file lines count:', envLines.length);
// Check for required OAuth variables (without showing values)
const requiredVars = ['GOOGLE_APP_SCRIPT_API_CLIENT_ID', 'GOOGLE_APP_SCRIPT_API_CLIENT_SECRET', 'GOOGLE_APP_SCRIPT_API_REFRESH_TOKEN'];
requiredVars.forEach(varName => {
const hasVar = envContent.includes(varName + '=');
console.log(` đ ${varName} present:`, hasVar);
});
}
} catch (fsError) {
console.log(' â ī¸ Could not check .env file:', fsError.message);
}
console.log('');
console.log('đ§ Troubleshooting steps:');
console.log('1. Check that your .env file contains valid OAuth credentials');
console.log('2. Verify your client ID and client secret are correct');
console.log('3. Ensure your refresh token is valid and not expired');
console.log('4. Follow the OAUTH_SETUP.md guide to obtain new credentials if needed');
console.log('5. Make sure the Google Apps Script API is enabled in your GCP project');
console.log('6. Check your internet connection and firewall settings');
console.log('7. Verify that the oauth-helper.js file exists and is accessible');
process.exit(1);
}
}
// Run the test 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('test-oauth.js'));
if (process.argv[1] && process.argv[1].endsWith('test-oauth.js')) {
console.log('đ Starting OAuth test...');
testOAuthAuthentication();
} else {
console.log('â Script not executed directly, skipping test');
}