39 lines
1.4 KiB
JavaScript
39 lines
1.4 KiB
JavaScript
#!/usr/bin/env node
|
|
// Renew Gmail Watch subscription (expires every 7 days)
|
|
// Run via cron every 6 days
|
|
|
|
import { google } from 'googleapis';
|
|
import { readFileSync, writeFileSync } from 'fs';
|
|
import { fileURLToPath } from 'url';
|
|
import { dirname, join } from 'path';
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
const CLIENT_ID = '689689127579-1it7eeinkckf1qgrvs2g9nvkjk1f960h.apps.googleusercontent.com';
|
|
const CLIENT_SECRET = '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';
|
|
const TOPIC = 'projects/key-utility-477818-t4/topics/upwork-email-notifications';
|
|
const TOKEN_FILE = join(__dirname, 'gmail-refresh-token.json');
|
|
|
|
const tokenData = JSON.parse(readFileSync(TOKEN_FILE, 'utf8'));
|
|
|
|
const oauth2Client = new google.auth.OAuth2(CLIENT_ID, CLIENT_SECRET);
|
|
oauth2Client.setCredentials({ refresh_token: tokenData.refresh_token });
|
|
|
|
const gmail = google.gmail({ version: 'v1', auth: oauth2Client });
|
|
|
|
const watchResponse = await gmail.users.watch({
|
|
userId: 'me',
|
|
requestBody: {
|
|
topicName: TOPIC,
|
|
labelIds: ['INBOX'],
|
|
labelFilterBehavior: 'INCLUDE',
|
|
},
|
|
});
|
|
|
|
console.log('Watch renewed:', JSON.stringify(watchResponse.data));
|
|
|
|
// Update expiration
|
|
tokenData.expiration = watchResponse.data.expiration;
|
|
tokenData.lastRenewal = new Date().toISOString();
|
|
writeFileSync(TOKEN_FILE, JSON.stringify(tokenData, null, 2));
|
|
console.log('Token file updated with new expiration');
|