Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to login to an external website with Google Apps Scripts?

I'm trying to login to a website so that I can pull back data to a Google docs spreadsheet. I've read various posts here, but I can't work out how to identify the data I need to pass.

The login page of the site has a form with the following fields.

<form action="https://fantasyfootball.telegraph.co.uk/premierleague/log-in/" method="post" id='reg-form'>
        <fieldset class='login'>

            <div class="required">
                <label for="email">Email:</label>
                <input type="text" name="email" id="email" class="input-text" size="10" maxlength="50" value="[email protected]" />
            </div>

            <div class="required">
                <label for="pass">Password:</label>
                <input type="password" name="pass" id="pass" class="input-password" size="10" maxlength="15" value="some-password" />
            </div>
        </fieldset>
        <div id="remember-me-container">
                <input type="checkbox" checked="checked" id="remember-me" name="remember-me" value="remember-me" />
                <label for="remember-me" id="remember-lbl">Remember me</label>

        </div>

        <input type="submit" id="submit-btn" value="Login" name='Submit' class='btn'/>
    </form>

I've tried the following script, but the sessionDetails always comes back as "Undefined."

// Returns the html of the page.
function sGetPage (sUrl) {

var url = "https://fantasyfootball.telegraph.co.uk/premierleague/log-in/";

// logging in, following http://stackoverflow.com/questions/21621019/google-apps-script-login-to-website-with-http-request
var payload =
{
 "email" : "[email protected]",
 "pass" : "asdf123",
 "submit-btn": "Login",
 "remember-me" : "remember-me" 
};

var options =
{
 "method" : "post",
 "payload" : payload,
 "followRedirects" : false
};

var login = UrlFetchApp.fetch( url, options);
var sessionDetails = login.getAllHeaders()['Set-Cookie'];
Logger.log(sessionDetails); 

var response = UrlFetchApp.fetch ("https://fantasyfootball.telegraph.co.uk/premierleague/leagues/view/8000912/4015677/", {"headers" : {"Cookie" : sessionDetails} });
var sHtml = response.getContentText();
Logger.log(sHtml); 

}

Any advice would be appreciated.

like image 290
Ian Shaw Avatar asked Aug 28 '14 16:08

Ian Shaw


1 Answers

I haven't ever been able to send sessionDetails back to the site as-is. I've been using RegEx to extract the relevant parts of sessionDetails and create a new cookie string, and send that back to the site. To find out what parts of the cookie are relevant, use your browser's network log (in the developer tools) to examine what your browser posts for the cookie, and compare that string to sessionDetails. I posted an example here.

var login = UrlFetchApp.fetch(url, options);
var sessionDetails = login.getAllHeaders()['Set-Cookie'];
Logger.log(sessionDetails); 
var cookie = sessionDetails.match(/Asp\.NetSessionId=[A-Z0-9]+;/)[0];  //modify this RegEx as needed

var response = UrlFetchApp.fetch(
  "https://example.com",
  {"headers" : {"Cookie" : cookie} }
);
like image 79
browly Avatar answered Oct 13 '22 13:10

browly