title: Login using UrlFetchApp function and scrape private data
body: <p>I need to scrape private data from portal which doesn't support API. 
  <code>IMPORTXML</code> can't do this because of login. </p>
  
  <p>I have a link with information of from &amp; to date and the content is table with cost data. I need to login and scrape simple table into my Google Sheet.</p>
  
  <p>I need to log in into this website:</p>
  
  <blockquote>
    <p><a href="https://www.glami.cz/registrace/prihlasit" rel="nofollow noreferrer">https://www.glami.cz/registrace/prihlasit</a></p>
  </blockquote>
  
  <p>and than scrape this url:</p>
  
  <blockquote>
    <p><a href="https://partner.glami.cz/s/e-commerce/days/866/?from=2016-12-01&amp;to=2016-12-09" rel="nofollow noreferrer">https://partner.glami.cz/s/e-commerce/days/866/?from=2016-12-01&amp;to=2016-12-09</a></p>
  </blockquote>
  
  <p>The form on this site is:</p>
  
  <pre><code>&lt;form action="/registrace/prihlasit/" method="post" id="frm-signIn"&gt;
  &lt;dl class="form"&gt;
      &lt;dt&gt;&lt;label for="frm-signIn-username" class="required"&gt;Emailová adresa&lt;/label&gt;&lt;/dt&gt;
      &lt;dd&gt;&lt;input type="text" name="username" id="frm-signIn-username" required data-nette-rules='[{"op":":filled","msg":"Prosím, vyplňte emailovou adresu."}]' class="text"&gt;&lt;/dd&gt;
      &lt;dt&gt;&lt;label for="frm-signIn-password" class="required"&gt;Heslo&lt;/label&gt;&lt;/dt&gt;
      &lt;dd&gt;&lt;input type="password" name="password" id="frm-signIn-password" required data-nette-rules='[{"op":":filled","msg":"Prosím, vyplňte heslo."}]' class="text"&gt;&lt;/dd&gt;
      &lt;dt&gt;&lt;/dt&gt;
      &lt;dd&gt;&lt;input type="submit" name="send" value="Přihlásit se" class="button"&gt;&lt;/dd&gt;
  &lt;/dl&gt;
  &lt;div&gt;&lt;input type="hidden" name="_do" value="signIn-submit"&gt;&lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>I have this code which works for other websites. In this case the response from logger is still "didnt log in".</p>
  
  <pre><code>function fetchAdminPage() {
     var url = "https://www.glami.cz/registrace/prihlasit";
     var options = {
        "method": "post",
        "payload": {
          'username': 'LOGIN',
          'password': 'PASSWORD',
          'send': 'Přihlásit se',
          '_do': 'signIn-submit',
          "testcookie": 1
        },
        "followRedirects": false
     };
     var response = UrlFetchApp.fetch(url, options);
     if ( response.getResponseCode() == 200 ) {
       // Incorrect user/pass combo
       Logger.log("didnt log in");
     } 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://partner.glami.cz/s/e-commerce/days/866/?from=2016-12-01&amp;to=2016-12-09";
          options = {
              "method": "get",
              // Set the cookies so that we appear logged-in
              "headers": {
                 "Cookie": cookies.join(';')
              }
          };
          response = UrlFetchApp.fetch(url, options);
       };
       Logger.log(response.getContentText()); 
     };     
  }
  </code></pre>
  
  <p>The problem has to be I guess somewhere in payload/options or in url adress. Can't figure out what's wrong and how to log in succesfully.</p>