title: Google Apps Script (GAS) Using urlfetchapp with username and password
body: <p>I am not a coder by nature and am self taught in GAS (only code I have used). I work for City College Norwich and I would like to create a script that automatically logs me in to their website so I can fetch timetable data and put it into a spreadsheet.</p>
  
  <p>After doing some research I have given up trying to figure it out so I am asking for help.</p>
  
  <p>I have tried this:</p>
  
  <pre><code>function getTimetables() {
   var url = "https://ccn.ac.uk/user/";
     var options = {
        "method": "post",
        "payload": {
        "user-login" : "username",
        "edit-pass" : "password",
        "BUTTON_Submit" : "Log In",
        },
        "testcookie": 1,
        "followRedirects": false
     };
     var response = UrlFetchApp.fetch(url, options);
     if ( response.getResponseCode() == 200 ) {
       // Incorrect user/pass combo
       Logger.log('Incorrect user/pass combo')
     } else if ( response.getResponseCode() == 302 ) {
       // Logged-in
       var headers = response.getAllHeaders();
       if ( typeof headers['Set-Cookie'] !== 'undefined' ) {
          // Make sure that we are working with an array of cookies
          var cookies = typeof headers['Set-Cookie'] == 'string' ? [ headers['Set-Cookie'] ] : headers['Set-Cookie'];
          for (var i = 0; i &lt; cookies.length; i++) {
             // We only need the cookie's value - it might have path, expiry time, etc here
             cookies[i] = cookies[i].split( ';' )[0];  
          };
  
          url = "https://mytimetable.ccn.ac.uk/timetable.aspx?week=30&amp;room=C5A";
          options = {
              "method": "get",
              // Set the cookies so that we appear logged-in
              "headers": {
                 "Cookie": cookies.join(';') 
              }
          } 
       }
     }
  }
  </code></pre>
  
  <p>Which return "Incorrect user/pass combo".</p>
  
  <p>And I have tried this:</p>
  
  <pre><code>function getTimetablev2() {
  var site = "https://ccn.ac.uk/user"
  var USERNAME = PropertiesService.getScriptProperties().getProperty('username');
  var PASSWORD = PropertiesService.getScriptProperties().getProperty('password');
  
  var url = PropertiesService.getScriptProperties().getProperty(site);
  
    var headers = {
      "Authorization" : "Basic " + Utilities.base64Encode(USERNAME + ':' + PASSWORD)
    };
  
  var params
  
   = {
      "method":"GET",
      "headers":headers
    };
  
  var response = 
  
  UrlFetchApp.fetch(site, params); 
    Logger.log(response.getResponseCode())
  }
  </code></pre>
  
  <p>Which return code 200 - failed to log in.</p>
  
  <p>If anyone can solve this for me I would be forever in your debt as it would save me loads of time. I have created a practical booking system where each teacher has their own spreadsheet with their timetable on and all bookings go to a master spreadsheet us technicians use. If I could automate generating their timetables it would be fantastic.</p>