问题
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.
I am trying to login to this website https://teslaclub.ch/clubdesk/www?action=login&
As it looks like the relevant login information is the following:
<form method="post" action="www" accept-charset="UTF-8">
<input type="hidden" name="action" value="login">
<input type="hidden" name="redirectUrl" value="HxgrZFvkpAK4n-IGMS4gSQ">
<table class="admin">
<tr>
<td class="label">Benutzername:</td>
<td class="field"><input id="userId" name="userId" type="text" value="my login name comes here"></td>
</tr>
<tr>
<td class="label">Passwort:</td>
<td class="field"><input id="password" name="password" type="password"></td>
</tr>
<tr>
<td class="error" colspan="2"></td>
</tr>
<tr>
<td class="comment" colspan="2" style="line-height: 28px;color:#2eab66;">
<a href="www?action=signupUser&">
Zugang anfordern</a>
<a href="www?action=recover&">
Passwort vergessen</a>/<a href="www?action=changePassword&">
ändern</a>
<input style="float: right;" type="submit" value="Anmelden">
</td>
</table>
</form>
I am trying to login like this (which i took from other examples and modified it):
function loginToSite(){
var url = "https://teslaclub.ch/clubdesk/www?action=login&"; //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);
}
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.
Your help would be highly appreciated.
Heinz
来源:https://stackoverflow.com/questions/52503332/login-to-website-using-google-apps-script