#!/usr/bin/env node /** * TickTick OAuth Test Helper * This script helps you complete the OAuth flow and get access tokens */ import { createServer } from 'http'; import { parse } from 'url'; import { TickTickAuth } from '../dist/auth/ticktick-auth.js'; // Load environment variables import dotenv from 'dotenv'; dotenv.config(); const config = { clientId: process.env.TICKTICK_CLIENT_ID, clientSecret: process.env.TICKTICK_CLIENT_SECRET, redirectUri: process.env.TICKTICK_REDIRECT_URI || 'http://localhost:3000/callback', }; if (!config.clientId || !config.clientSecret) { console.error('āŒ Missing TICKTICK_CLIENT_ID or TICKTICK_CLIENT_SECRET'); console.error('Run: npm run setup-env first'); process.exit(1); } const auth = new TickTickAuth(config); // Create a simple HTTP server to handle OAuth callback const server = createServer(async (req, res) => { const urlParts = parse(req.url, true); if (urlParts.pathname === '/callback') { const { code, error } = urlParts.query; if (error) { res.writeHead(400, { 'Content-Type': 'text/html' }); res.end(`

āŒ OAuth Error

Error: ${error}

Please try again.

`); return; } if (code) { try { console.log('\nšŸ”„ Exchanging authorization code for tokens...'); const tokens = await auth.exchangeCodeForToken(code); console.log('\nāœ… OAuth Success!'); console.log('šŸ“‹ Add these to your .env file:'); console.log(`TICKTICK_ACCESS_TOKEN=${tokens.access_token}`); console.log(`TICKTICK_REFRESH_TOKEN=${tokens.refresh_token}`); res.writeHead(200, { 'Content-Type': 'text/html' }); res.end(`

āœ… OAuth Success!

Your TickTick MCP Server is now authenticated.

Check your terminal for the access tokens.

You can close this window and stop the server (Ctrl+C).

`); console.log('\nšŸŽ‰ Authentication complete!'); console.log('You can now stop this server (Ctrl+C) and test your MCP server.'); } catch (error) { console.error('\nāŒ Token exchange failed:', error.message); res.writeHead(500, { 'Content-Type': 'text/html' }); res.end(`

āŒ Token Exchange Failed

Error: ${error.message}

`); } } } else { res.writeHead(404, { 'Content-Type': 'text/plain' }); res.end('Not found'); } }); // Start server const port = new URL(config.redirectUri).port || 3000; server.listen(port, () => { console.log('šŸš€ TickTick OAuth Helper Started'); console.log('==============================='); console.log(`šŸ“” Listening on port ${port}`); console.log(''); console.log('šŸ”— Open this URL in your browser to authenticate:'); console.log(auth.getAuthorizationUrl()); console.log(''); console.log('šŸ’” After authentication, you\'ll get access tokens to add to your .env file'); console.log('ā¹ļø Press Ctrl+C to stop this server'); }); // Handle graceful shutdown process.on('SIGINT', () => { console.log('\n\nšŸ›‘ OAuth helper stopped'); server.close(); process.exit(0); });