Skip to main content
Glama
auth.ts1.72 kB
import { google } from 'googleapis'; import { OAuth2Client } from 'google-auth-library'; import * as fs from 'fs'; import * as path from 'path'; import { SCOPES, REDIRECT_URI } from './config.js'; import * as dotenv from 'dotenv'; import * as os from 'os'; const CONFIG_DIR = path.join(os.homedir(), '.config', 'google-webmaster-mcp'); if (!fs.existsSync(CONFIG_DIR)) { fs.mkdirSync(CONFIG_DIR, { recursive: true }); } // Load .env files while silencing dotenv's stdout noise const withSilencedLogs = (fn: () => void) => { const originalLog = console.log; console.log = () => {}; try { fn(); } finally { console.log = originalLog; } }; withSilencedLogs(() => dotenv.config()); // Load .env from config dir if it exists const globalEnvPath = path.join(CONFIG_DIR, '.env'); if (fs.existsSync(globalEnvPath)) { withSilencedLogs(() => dotenv.config({ path: globalEnvPath })); } const TOKEN_PATH = path.join(CONFIG_DIR, 'token.json'); export async function getAuthClient(): Promise<OAuth2Client> { const clientId = process.env.GOOGLE_CLIENT_ID; const clientSecret = process.env.GOOGLE_CLIENT_SECRET; if (!clientId || !clientSecret) { throw new Error('Missing GOOGLE_CLIENT_ID or GOOGLE_CLIENT_SECRET in .env'); } const oAuth2Client = new google.auth.OAuth2(clientId, clientSecret, REDIRECT_URI); if (fs.existsSync(TOKEN_PATH)) { const token = fs.readFileSync(TOKEN_PATH, 'utf-8'); oAuth2Client.setCredentials(JSON.parse(token)); } return oAuth2Client; } export function saveToken(tokens: any) { fs.writeFileSync(TOKEN_PATH, JSON.stringify(tokens, null, 2)); console.log('Token saved to', TOKEN_PATH); }

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/ehukaimedia/google-webmaster-mcp'

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