title: The OAuth Client was not found - Google Apps Script - BigQuery
body: <p>I am trying to use App Script to load BigQuery results into a Google Spreadsheet. Here is my Code</p>
  
  <pre><code>function runQuery() {
  var projectId = 'xxxxx';
  
  var request = {
    query: 'select * from ASRLogs.LocationBasedClicks;'
  };
  
  var queryResults = BigQuery.Jobs.query(request,projectId);
  var jobId = queryResults.jobReference.jobId;
  
  // Check on status of the Query Job. 
  var sleepTimeMs = 500;
  
  while(!queryResults.jobComplete)
  {
     Utilities.sleep(sleepTimeMs);
     sleepTimeMs *=2;
     queryResults = BigQuery.Jobs.getQueryResults(projectId, jobId);
  }
  
  // Get all the rows of the result. 
  var rows = queryResults.rows;
  while (queryResults.pageToken) {
     queryResults = BigQuery.Jobs.getQueryResults(projectId, jobId, {
         pageToken: queryResults.pageToken
     });
     rows = rows.concat(queryResults.rows);
  }
  
  if (rows) {
       var spreadsheet = SpreadsheetApp.create("BigQuery Results");
       var sheet = spreadsheet.getActiveSheet();
  
       // Append the headers
       var headers = queryResults.schema.fields.map(function(field) {
         return field.name;
       });
       sheet.appendRow(headers)
  
       // Append the results. 
       var data = new Array(rows.length);
       for (var i = 0; i &lt; rows.length; i ++){
         var cols = rows[i].f;
         data[i] = new Array(cols.length);
         for (var j =0; j &lt; cols.length; j++){
           data[i][j] = cols[j].v;
         }
       }
       sheet.getRange(2,1,rows.length,headers.length).setValue(data);
  
       Logger.log('Results spreadsheet created: %s', spreadsheet.getUrl());
  } else {
    Logger.log('No rows returned.')
  }
  }
  </code></pre>
  
  <p>The error I get is </p>
  
  <blockquote>
    <p>Error: invalid_client<br>
    The OAuth client was not found.<br>
    Request Details<br>
    - cookie_policy_enforce=false<br>
    - scope=<a href="https://www.googleapis.com/auth/bigquery" rel="nofollow">https://www.googleapis.com/auth/bigquery</a> <a href="https://www.googleapis.com/auth/spreadsheets" rel="nofollow">https://www.googleapis.com/auth/spreadsheets</a><br>
    - response_type=code gsession<br>
    - redirect_uri=<a href="https://script.google.com/oauthcallback" rel="nofollow">https://script.google.com/oauthcallback</a><br>
    - access_type=offline<br>
    - approval_prompt=force<br>
    - state=ACjPJvHwuS-sspO-j9b5vlH_Ul4VokI3QRANL-gwa7YWxz6-RFelZBuLQ2aiiGldHRgR89sMnvlgpsmSOnlquEY45oTt1IgZHWfoWq5e52Jf6l_G-5yPpPwCf40Dkv_JYR9welQPrQ<br>
    - client_id=734978265744@developer.gserviceaccount.com<br>
    - hl=en  </p>
  </blockquote>
  
  <p>Am I missing something ? I have done the following. </p>
  
  <ol>
  <li>Enabled BigQuery API in Google Serveices</li>
  <li>Enabled BigQuery in the developer console for the project. </li>
  </ol>