#!/usr/bin/env node const fs=require('fs');const https=require('https');const {homedir}=require('os'); function httpsRequest(url, options={}){return new Promise((res,rej)=>{const req=https.request(url,options,r=>{let d='';r.on('data',c=>d+=c);r.on('end',()=>{if(r.statusCode>=400)rej(new Error(`HTTP ${r.statusCode}: ${d}`));else try{res(JSON.parse(d))}catch(e){res(d)};});});req.on('error',rej);if(options.body)req.write(options.body);req.end();});} (async()=>{ try{ const id=process.argv[2]; if(!id) throw new Error('Usage: mark_read_gmail.cjs '); const credsPath=require('path').join(homedir(),'.openclaw/secrets/google-oauth.json'); const creds=JSON.parse(fs.readFileSync(credsPath,'utf8')); const body=new URLSearchParams({client_id:creds.client_id,client_secret:creds.client_secret,refresh_token:creds.refresh_token,grant_type:'refresh_token'}).toString(); const tokenResp=await httpsRequest('https://oauth2.googleapis.com/token',{method:'POST',headers:{'Content-Type':'application/x-www-form-urlencoded','Content-Length':Buffer.byteLength(body)},body}); const token=tokenResp.access_token; const url=`https://gmail.googleapis.com/gmail/v1/users/me/messages/${id}/modify`; const patchBody=JSON.stringify({removeLabelIds:['UNREAD']}); await httpsRequest(url,{method:'POST',headers:{Authorization:`Bearer ${token}`,'Content-Type':'application/json'},body:patchBody}); console.log('Marked as read:',id); }catch(err){console.error('Error:',err.message);process.exit(1);} })();