← back to Gmc Titlefix

step-a-create-datasource.js

27 lines

// GATED WRITE — creates a supplemental data source in Merchant Center (Merchant API v1).
// v1 rejects feedLabel/contentLanguage at create-time on a supplemental source, so we create
// an UNBOUND supplemental data source ({}), which merges overrides onto products by offerId.
const {token,MERCHANT}=require('./_auth');
(async()=>{
  const tok=await token();
  const url=`https://merchantapi.googleapis.com/datasources/v1/accounts/${MERCHANT}/dataSources`;
  const shapes=[
    { displayName:'DW Sample Title Overrides', supplementalProductDataSource:{} },
    { displayName:'DW Sample Title Overrides', supplementalProductDataSource:{ contentLanguage:'en' } },
    { displayName:'DW Sample Title Overrides', supplementalProductDataSource:{ contentLanguage:'en', feedLabel:'US' } },
  ];
  for(const body of shapes){
    const r=await fetch(url,{method:'POST',headers:{Authorization:'Bearer '+tok,'Content-Type':'application/json'},body:JSON.stringify(body)});
    const j=await r.json();
    console.log('shape='+JSON.stringify(body.supplementalProductDataSource)+' -> HTTP '+r.status);
    if(r.status>=200 && r.status<300 && j.name){
      console.log(JSON.stringify(j,null,2));
      console.log('\n>>> SAVE THIS dataSource name for steps B/D:\n'+j.name);
      return;
    } else {
      console.log('  '+(j.error?.message||JSON.stringify(j)).slice(0,160));
    }
  }
  console.log('\n>>> all shapes failed — see errors above');
})();