Files
openclaw/skills/gmail-reader/exchange-code.cjs
Clawd Bot ca9b510922 chore: align with upstream openclaw/openclaw and overlay local additions
- Reset master to upstream/main (16,697 commits)
- Overlay 2,271 local-only files (skills, tools, workspace, configs, apps)
- Restore IDENTITY.md and USER.md templates
- Build verified, gateway running, Discord working

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-03 07:40:46 +01:00

84 lines
2.4 KiB
JavaScript

#!/usr/bin/env node
const fs = require('fs');
const https = require('https');
const { homedir } = require('os');
const { join } = require('path');
function httpsRequest(url, options = {}) {
return new Promise((resolve, reject) => {
const req = https.request(url, options, (res) => {
let data = '';
res.on('data', (chunk) => (data += chunk));
res.on('end', () => {
if (res.statusCode >= 400) {
reject(new Error(`HTTP ${res.statusCode}: ${data}`));
} else {
resolve(JSON.parse(data));
}
});
});
req.on('error', reject);
if (options.body) req.write(options.body);
req.end();
});
}
async function main() {
const code = process.argv[2];
if (!code) {
console.error('❌ Usage: node exchange-code.cjs <authorization-code>');
process.exit(1);
}
const credsPath = join(homedir(), '.openclaw/secrets/google-oauth.json');
const creds = JSON.parse(fs.readFileSync(credsPath, 'utf8'));
console.log('🔄 Exchanging authorization code for tokens...\n');
const body = new URLSearchParams({
client_id: creds.client_id,
client_secret: creds.client_secret,
code: code,
redirect_uri: 'http://localhost:8080',
grant_type: 'authorization_code',
}).toString();
const data = await httpsRequest('https://oauth2.googleapis.com/token', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(body),
},
body,
});
console.log('✅ Tokens received!\n');
// Update credentials file
const newCreds = {
client_id: creds.client_id,
client_secret: creds.client_secret,
refresh_token: data.refresh_token || creds.refresh_token,
access_token: data.access_token,
expiry_date: Date.now() + (data.expires_in * 1000),
};
fs.writeFileSync(credsPath, JSON.stringify(newCreds, null, 2));
console.log('💾 Updated credentials saved to:');
console.log(` ${credsPath}\n`);
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n');
console.log('🎉 Google OAuth updated successfully!\n');
console.log('Du har nu adgang til:');
console.log(' ✅ Gmail (read + send)');
console.log(' ✅ Google Sheets (readonly)');
console.log(' ✅ Google Drive (readonly)\n');
}
main().catch((err) => {
console.error('❌ Error:', err.message);
process.exit(1);
});