title: Login to website using Google Apps script
body: <p>Login in to a website using Google Apps Script has been discussed here previously and I most probably have seen them all and tried them all. But nothing seems to work. I must oversee something and would appreciate your help.</p>
  
  <p>I am trying to login to this website <a href="https://teslaclub.ch/clubdesk/www?action=login&amp;" rel="nofollow noreferrer">https://teslaclub.ch/clubdesk/www?action=login&amp;</a></p>
  
  <p>As it looks like the relevant login information is the following:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;form method="post" action="www" accept-charset="UTF-8"&gt;
      &lt;input type="hidden" name="action" value="login"&gt;
  &lt;input type="hidden" name="redirectUrl" value="HxgrZFvkpAK4n-IGMS4gSQ"&gt;
  &lt;table class="admin"&gt;
          &lt;tr&gt;
              &lt;td class="label"&gt;Benutzername:&lt;/td&gt;
              &lt;td class="field"&gt;&lt;input id="userId" name="userId" type="text" value="my login name comes here"&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td class="label"&gt;Passwort:&lt;/td&gt;
              &lt;td class="field"&gt;&lt;input id="password" name="password" type="password"&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td class="error" colspan="2"&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td class="comment" colspan="2" style="line-height: 28px;color:#2eab66;"&gt;
                  &lt;a href="www?action=signupUser&amp;"&gt;
                      Zugang anfordern&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
                  &lt;a href="www?action=recover&amp;"&gt;
                      Passwort vergessen&lt;/a&gt;/&lt;a href="www?action=changePassword&amp;"&gt;
                  ändern&lt;/a&gt;
                  &lt;input style="float: right;" type="submit" value="Anmelden"&gt;
              &lt;/td&gt;
      &lt;/table&gt;
  &lt;/form&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>I am trying to login like this (which i took from other examples and modified it):</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function loginToSite(){
    var url = "https://teslaclub.ch/clubdesk/www?action=login&amp;"; //change this.
    var payload = {
      "userId":"my login name comes here", //change this.
      "password":"my password comes here" //and change this. done. no need to enter anything elsewhere.
    }; 
    var opt = {
      "payload":payload,
      "method":"post"
    };
    var response = UrlFetchApp.fetch(url, opt);
    if ( response.getResponseCode() == 200 ) { //could not log in.
      var result = "Couldn't login. Please make sure your username/password is correct.";
    } 
    else if ( response.getResponseCode() == 303 ) { //login was successful. you might receive 302 response code as well depending upon the site. So try changing it to 302.
       var result = "Logged in successfully";
       var cookie = response.getAllHeaders()['Set-Cookie'];     
       var header = {
         'Cookie':cookie[1] //taking the second cookie because when redirected, a new cookie is set. If the page does not redirect, you might need to use cookie[0] here. Try it out yourself!
       };
    }
  Logger.log(result);
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>I always get a 200 response back. Therefore I think I am missing a function to "press" the login (Anmelden) button. But I am not sure how I could do that.</p>
  
  <p>Your help would be highly appreciated.</p>
  
  <p>Heinz</p>