title: Using Google Sheets API from within Apps Script for same Sheet
body: <p>Using an Apps Script that's bound to a Spreadsheet to effect the sheet is very straight forward, via the SpreadsheetApp global. However, there's some functionality, such as getting/setting filters on a sheet, that can only be accessed from the Google Sheets REST API.</p>
  
  <p>I've seen an example that uses UrlFetchApp from within an Apps Script, to call the Google Sheet API, but it's written as if the Apps Script is not actually bound to a particular Spreadsheet.</p>
  
  <pre><code>function getSheetBasicFilter(ssId, sheetId) {
    var params = {
      headers: {
        "Authorization": "Bearer " + ScriptApp.getOAuthToken()
      }
    };
    var base = "https://sheets.googleapis.com/v4/spreadsheets/";
    var partialRespParams = "?fields=sheets%2FbasicFilter";
    var resp = UrlFetchApp.fetch(base + ssId + partialRespParams, params);
    var sheets = JSON.parse(resp.getContentText()).sheets;
    for (var i = 0; i &lt; sheets.length; i++) {
      if (sheets[i].basicFilter 
          &amp;&amp; (sheets[i].basicFilter.range.sheetId == sheetId 
              || (!sheets[i].basicFilter.range.sheetId &amp;&amp; sheetId == 0))) {
        return sheets[i].basicFilter;
      }
    }
    return null;
  }
  </code></pre>
  
  <p>When I attempt to call this function from within a Spreadsheet Bound Apps Script, I get an error 'Request had insufficient authentication scopes'.</p>
  
  <p>Since the script is running as the user that owns the document, and ScriptApp.getOAuthToken() is being used, it's not clear why this error is occurring.</p>
  
  <p>Any help would be appreciated.</p>