title: Export (or print) with a google script new version of google spreadsheets to pdf file, using pdf options
body: <p>I'm trying to make a google script for exporting (or printing) a new version of google spreadsheet (or sheet) to pdf, with page parameters (portrait/landscape, ...)</p>
  
  <p>I've researched about this and found a possible solution <a href="https://stackoverflow.com/questions/21168167/include-sheet-names-when-exporting-google-spreadsheets-to-pdf-with-google-apps-s/21172633#21172633">here</a>.
  There are several similar solutions like this, but only work with old version of google spreadsheet.</p>
  
  <p>Please, consider this code:</p>
  
  <pre><code>function exportAsPDF() {
    //This code runs from a NEW version of spreadsheet 
  
    var oauthConfig = UrlFetchApp.addOAuthService("google");
    oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
    oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope=https://spreadsheets.google.com/feeds/");
    oauthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
    oauthConfig.setConsumerKey("anonymous");  oauthConfig.setConsumerSecret("anonymous");
    var requestData = { "method": "GET", "oAuthServiceName": "google","oAuthUseToken": "always" };
  
    var ssID1="0AhKhywpH-YlQdDhXZFNCRFROZ3NqWkhBWHhYTVhtQnc"; //ID of an Old version of spreadsheet
    var ssID2="10xZX9Yz95AUAPu92BkBTtO0fhVk9dz5LxUmJQsJ7yPM"; //ID of a NEW version of spreadsheet 
  
    var ss1 = SpreadsheetApp.openById(ssID1);  //Old version ss object
    var ss2 = SpreadsheetApp.openById(ssID2);  //New version ss object
    var sID1=ss1.getActiveSheet().getSheetId().toString();  // old version sheet id
    var sID2=ss2.getActiveSheet().getSheetId().toString();  // new version sheet id
  
    //For Old version, this runs ok.
    var url1 = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key="+ssID1+"&amp;gid="+sID1+"&amp;portrait=true"+"&amp;exportFormat=pdf";
    var result1 = UrlFetchApp.fetch(url1 , requestData);
    var contents1=result1.getBlob();
    var pdfFile1=DriveApp.createFile(contents1).setName("FILE1.pdf");
  
    //////////////////////////////////////////////
    var url2 = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key="+ssID2+"&amp;gid="+sID2+"&amp;portrait=true"+"&amp;exportFormat=pdf";
    var result2 = UrlFetchApp.fetch(url2 , requestData);
    var contents2=result2.getBlob();
    var pdfFile2=DriveApp.createFile(contents2).setName("FILE2.pdf");
  }
  </code></pre>
  
  <p>It works right and generates the file “FILE1.pdf”, that can be opened correctly. But for the new version of spreadsheet, it results in error 302 (<em>truncated server response</em>) at “<em>var result2 = UrlFetchApp.fetch(url2 , requestData);</em>”. Well, it’s ok because the url format for the new version doesn’t include the “key” argument. A correct url for new versions must be like <em><code>"https://docs.google.com/spreadsheets/d/"+ssID2+"/export?gid="+sID2+"&amp;portrait=true&amp;format=pdf"</code></em> </p>
  
  <p>Using this for url2 (<em><code>var url2 = "https://docs.google.com/spreadsheets/d/"+ssID2+"/export?gid="+sID2+"&amp;portrait=true&amp;format=pdf"</code></em>) it fails again with error  “<em>Authorization can’t be performed for service: google</em>”. 
  Well, this error could be due to an incorrect scope for the <em>RequestTokenUrl</em>. I’ve found the alternative scope <code>https://docs.google.com/feeds</code> and set it: <code>oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope=https://docs.google.com/feed/");</code> 
  After the code runs again, a new error happens at the line with <code>UrlFetchApp.fetch(url2 , requestData);</code>: “<em>Error OAuth</em>” … I don’t know how to continue … I’ve tested hundreds of variations without good results.</p>
  
  <p>Any ideas? is correct the scope <em>docs.google.com/feeds</em> for new version of spreadsheets? is correct the <em>oauthConfig</em>?</p>
  
  <p>Thanks in advance.</p>