Skip to main content
Glama
auth.js3.35 kB
import { google } from "googleapis"; import { promises as fs } from "fs"; import path from "path"; import readline from "readline"; // Scopes for Google Drive API const SCOPES = ['https://www.googleapis.com/auth/drive']; const TOKEN_PATH = 'token.json'; const CREDENTIALS_PATH = 'credentials.json'; async function authorize() { try { // Load client secrets from a local file const credentials = JSON.parse(await fs.readFile(CREDENTIALS_PATH, 'utf8')); const { client_secret, client_id, redirect_uris } = credentials.installed || credentials.web; const oAuth2Client = new google.auth.OAuth2( client_id, client_secret, redirect_uris[0] ); // Check if we have previously stored a token try { const token = JSON.parse(await fs.readFile(TOKEN_PATH, 'utf8')); oAuth2Client.setCredentials(token); console.log('✓ Authentication token loaded successfully!'); return oAuth2Client; } catch (err) { // No token file, need to get new token return await getNewToken(oAuth2Client); } } catch (error) { console.error('Error loading credentials:', error); console.log('\nMake sure you have downloaded your credentials.json file from Google Cloud Console'); console.log('and placed it in the same directory as this script.'); process.exit(1); } } async function getNewToken(oAuth2Client) { const authUrl = oAuth2Client.generateAuthUrl({ access_type: 'offline', scope: SCOPES, }); console.log('\n🔐 Authorize this app by visiting this URL:'); console.log(authUrl); console.log('\nAfter authorization, you will get a code. Enter it below:'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); return new Promise((resolve, reject) => { rl.question('Enter the code from that page here: ', async (code) => { rl.close(); try { const { tokens } = await oAuth2Client.getToken(code); oAuth2Client.setCredentials(tokens); // Store the token to disk for later program executions await fs.writeFile(TOKEN_PATH, JSON.stringify(tokens)); console.log('✓ Token stored to', TOKEN_PATH); console.log('✓ Authentication complete!'); resolve(oAuth2Client); } catch (error) { console.error('Error retrieving access token:', error); reject(error); } }); }); } async function testConnection(auth) { const drive = google.drive({ version: 'v3', auth }); try { const response = await drive.files.list({ pageSize: 5, fields: 'files(id,name)', }); console.log('\n✓ Google Drive connection test successful!'); console.log('Recent files:'); response.data.files.forEach(file => { console.log(` - ${file.name} (${file.id})`); }); } catch (error) { console.error('Error testing Drive connection:', error); } } // Main execution async function main() { console.log('🚀 Setting up Google Drive authentication...\n'); try { const auth = await authorize(); await testConnection(auth); console.log('\n✅ Setup complete! You can now run your MCP server with:'); console.log('node server.js'); } catch (error) { console.error('Setup failed:', error); process.exit(1); } } main();

Latest Blog Posts

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/Raincode-Bahrain/mcp-drive'

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