6

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="my.name@address.com" />
            </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" : "ian.shaw@iee.org",
 "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.

Ian Shaw
  • 237
  • 4
  • 9
  • There an example here of handling cookies with apps script, might be what you're looking for http://stackoverflow.com/questions/19567105/how-to-fetch-a-wordpress-admin-page-using-google-apps-script – AshClarke Aug 28 '14 at 23:54
  • Thanks, I hadn't turned up that link. I've realised I need to include all the form fields, so I've added "submit-btn": "Login" to the options list, but it still hasn't worked. – Ian Shaw Aug 29 '14 at 13:48
  • Am i right to assume it's the input's "id" & "value" that needs to go in the options? I'm getting back a response code of 200, and no cookie data in getAllHeaders(). I've manually verified that the password is correct, so I'm still stumped. – Ian Shaw Aug 29 '14 at 14:06
  • @IanShaw Did you figure this out? – shivavelingker Dec 12 '18 at 23:23

1 Answers1

2

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} }
);
browly
  • 352
  • 6
  • 11