4

I am fairly new to Google Apps Script. I am using Google's functionality to access the DFA/DCM Trafficking and Reporting API through App Scripts without having to use OAuth.

When I run the DCM Report to then convert into google sheets, I am not able to figure out how to use either urls i'm supplied with to download the CSV.

Here is the code i'm using.

  var file =  DCM.Reports.run(profile.profileId,30792432);
  var file2 =  DCM.Files.get(30792432, file.id);
  //wait till running of the report is complete.
  file2 =  DCM.Files.get(30792432, file.id);
  var response = UrlFetchApp.fetch(file2.urls.browserUrl);

I also try using:

  file2.urls.apiUrl(); 

for the UrlFetchApp service, but that didn't work either.

Any help on how to execute the url to download the file as an object where I can paste into google sheets would be greatly appreciated.

Richard Klein
  • 53
  • 1
  • 4
  • What response do you get back? What's the output of `Logger.log(response)`? – Tesseract Feb 22 '16 at 01:12
  • HTTP, i've copied it to this word doc. https://docs.google.com/document/d/1xyib3Tn39AxzcD0Lrw_7N90CLTd11gxWCdCwVp4BnFE/edit?usp=sharing – Richard Klein Feb 22 '16 at 01:38
  • The html code in your word doc is incomplete. There is no actual dcm data in there. Are you sure you copied the entire thing? – Tesseract Feb 22 '16 at 01:44
  • I just exported the full dataset into the word doc so you can see, now it's complete. You can check again. There is no DCM data, (as far as i can tell). The closest I can get is a the BrowserURL which gives my a window to download the csv file. I'd share the url, but it has some sensitive information. – Richard Klein Feb 22 '16 at 01:51
  • What about `apiUrl`? Does that give you any data? – Tesseract Feb 22 '16 at 01:55
  • It seems like I need to authenticate myself to get in using the APIurl. This is the error i get when trying to use it. "Request failed for https://www.googleapis.com/dfareporting/v2.0/reports/30792432/files/216627769?alt=media returned code 401. Truncated server response: Login Required (use muteHttpExceptions option to examine full response)" This is what i've been trying to follow, but i'm not sure how to using google apps script language and UrlFetchApp. https://developers.google.com/doubleclick-advertisers/reporting/guides/download_reports – Richard Klein Feb 22 '16 at 02:00
  • Maybe `ScriptApp.getOAuthToken` is sufficient to get you a valid token. https://developers.google.com/apps-script/reference/script/script-app#getOAuthToken() Then you can just add that to the request headers when calling `UrlFetchApp.fetch`. https://developers.google.com/doubleclick-advertisers/reporting/guides/download_reports For example: `UrlFetchApp.fetch(url, { headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken() } });` – Tesseract Feb 22 '16 at 02:10
  • It worked! UrlFetchApp.fetch(file2.url.apiurl, { headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken() } }); It returned the document in CSV format! – Richard Klein Feb 22 '16 at 02:40

1 Answers1

1

Add the ScriptApp authorization bearer as a header in the parameters while using the apiurl call. Something like:

var token = ScriptApp.getOAuthToken();

var headersOptions = { 
  Authorization : 'Bearer '  + token
  };

var options = { 
  headers : headersOptions
  };

var csvDoc = UrlFetchApp.fetch(file2.url.apiurl, options);
Gerneio
  • 1,290
  • 10
  • 13