← back to Gmc 425 Supplemental Feed

probe-merchant-api.mjs

25 lines

/** probe-merchant-api.mjs — READ-ONLY. Can the SA reach the newer Merchant API dataSources?
 * If yes, a supplemental dataSource is the upload path for the 4,468 SUPP_FIXABLE. */
import fs from 'node:fs'; import crypto from 'node:crypto';
const HOME=process.env.HOME, ACCT='146735262';
const SA_PATH=HOME+'/Projects/secrets-manager/gmc-sa-146735262.json';
const b64=b=>Buffer.from(b).toString('base64').replace(/=/g,'').replace(/\+/g,'-').replace(/\//g,'_');
const SA=JSON.parse(fs.readFileSync(SA_PATH,'utf8'));const now=Math.floor(Date.now()/1000);
const si=b64(JSON.stringify({alg:'RS256',typ:'JWT'}))+'.'+b64(JSON.stringify({iss:SA.client_email,scope:'https://www.googleapis.com/auth/content',aud:'https://oauth2.googleapis.com/token',iat:now,exp:now+3600}));
const s=crypto.createSign('RSA-SHA256');s.update(si);const jwt=si+'.'+b64(s.sign(SA.private_key));
const tr=await fetch('https://oauth2.googleapis.com/token',{method:'POST',headers:{'Content-Type':'application/x-www-form-urlencoded'},body:new URLSearchParams({grant_type:'urn:ietf:params:oauth:grant-type:jwt-bearer',assertion:jwt})});
const tok=(await tr.json()).access_token; const H={Authorization:`Bearer ${tok}`};
// 1) list dataSources (Merchant API)
const r=await fetch(`https://merchantapi.googleapis.com/datasources/v1/accounts/${ACCT}/dataSources`,{headers:H});
const j=await r.json();
console.log('dataSources.list HTTP',r.status);
if(r.status===200){
  const ds=j.dataSources||[];
  console.log(`  ${ds.length} data sources:`);
  for(const d of ds) console.log(`   - ${d.name?.split('/').pop()} "${d.displayName}" type=${Object.keys(d).find(k=>/Input|Source/i.test(k))||'?'} ${d.primaryProductDataSource?'PRIMARY':d.supplementalProductDataSource?'SUPPLEMENTAL':''}`);
  console.log('\n=> Merchant API reachable. A SUPPLEMENTAL product dataSource can be created here to overlay excluded_destination on the $4.25 sample offers.');
}else{
  console.log('  body:',JSON.stringify(j).slice(0,400));
  console.log('\n=> If 403/PERMISSION_DENIED: SA lacks Merchant API access (needs enable + role). If 404: account path differs. This is the blocker to resolve for the supplemental path.');
}