title: GAS - Authentication w/ UrlFetchApp - Form to Spreadsheet
body: <p>I am testing the functionality of UrlFetchApp and passing data from a Form and its Spreadsheet. I know it's possible to do this another way, however I am testing the functionality of UrlFetchApp (first time using it) within google scripts themselves, and want to get it to work with this method.</p>
  
  <p>Here's the scenario I got, add a bound script to a Form App as so:</p>
  
  <pre><code>function makeRequest() 
  {
    var webAppUrl = "https://script.google.com/macros/s/WebAppID/exec";
  
    var auth = ScriptApp.getOAuthToken();
    var header = { 'Authorization': 'Bearer ' +  auth };
    var options = { 'method':'post', 'headers':header };
  
    var resp = UrlFetchApp.fetch(webAppUrl, options);
  
    Logger.log(resp);
  }
  </code></pre>
  
  <p>Add a bound script to the attached spreadsheet:</p>
  
  <pre><code>function doPost()
  {  
    var ss = SpreadsheetApp.openById('ssID');
    var name = ss.getName();
    return ContentService.createTextOutput(name);
  }
  </code></pre>
  
  <p>And then publish this second script attached to the sheet as a web app with only myself to have access. </p>
  
  <p>Currently the above code does not work. The following error appears on the Form side of the script:</p>
  
  <blockquote>
    <p>Request failed for
    <a href="https://script.google.com/macros/s/WebAppID/exec" rel="nofollow">https://script.google.com/macros/s/WebAppID/exec</a>
    returned code 401. Truncated server response:  
    Unauthorized   <H1>Unauthorized</H1> <H2>Error 401</H2> 
     (use muteHttpExceptions option to examine full response) (line
    12, file "Code")</p>
  </blockquote>
  
  <p>Fails on the UrlFetchApp line of code.</p>
  
  <p>However, if I remove the header option, then publish the web app for public use, this works just fine. Obviously this is not wanted.</p>
  
  <p>What am I missing regarding authentication between scripts that I own?</p>
  
  <p>Side Notes:</p>
  
  <p>Anyone know why SpreadsheetApp.getActiveSheet() doesn't work when run in this fashion? That script is directly bound to a google sheet, so kind of odd.</p>