title: Automated OAuth2 token not working - Google Apps Script
body: <p>I am trying to run a google apps script, in a document, that sends an email with an attached google spreadsheet as an .xlsx file automatically, running every few hours.</p>
  
  <p>Below is the solution that works if I use a manual OAuth2 code coming from the google OAuth2 playground :</p>
  
  <pre><code>function downloadXLS() {
    var AUTH_TOKEN = "xxxx"; 
    var auth = "AuthSub token=\"" + AUTH_TOKEN + "\"";
    var file = Drive.Files.get('xxxx');
    var response = UrlFetchApp.fetch('https://docs.google.com/spreadsheets/d/xxx/export?format=xlsx',{headers: {Authorization: auth}});      
    var doc = response.getBlob();
    app = DriveApp.createFile(doc).setName(file.title + '.xls')
    MailApp.sendEmail("xxx@xxx.com", "oh man", " Body", { attachments: app })
  }    
  </code></pre>
  
  <p>To try to auto-generate the authorization token I followed exactly all the steps here:</p>
  
  <p><a href="https://github.com/googlesamples/apps-script-oauth2" rel="nofollow">https://github.com/googlesamples/apps-script-oauth2</a></p>
  
  <p>Changing on the script :
        .setClientId('...')
        .setClientSecret('...')
  I also put in the URI the the Project Id inside the <a href="https://script.google.com/macros/d/myprojectkey/usercallback" rel="nofollow">https://script.google.com/macros/d/myprojectkey/usercallback</a> of the google developer console </p>
  
  <p>But when i run the function makeRequest() it tells me : "Access not granted or expired"</p>
  
  <p>So i wonder which step i missed.</p>
  
  <p>Do you have any clue on what is going on ?</p>
  
  <p>Help is much appreciated,
  Thanks</p>