2

I am interested in creating a google app script that on run would login into a specific website (third-party) and complete certain functions within the website (pressing buttons/copying text). After browsing the stackoverflow and other forums I have created a script that allows me to login into my website (source1 source2). However, I am having difficulties staying logged in and managing the data.

//The current code is just testing if I can get data from within the website.
//The results are displayed in a google app.

        function doGet() {
          var app = UiApp.createApplication(); 
          app.add(app.createLabel(display_basic_data()));
          return app;
        }

        //logins into website and displays data
        function display_basic_data() {
          var data;
          var url = "http://www.website.bla/users/sign_in";
          var payload = {"user[username]":"usr","user[password]":"ps"};
          var opt ={"method":"post","payload":payload, "followRedirects" : false};
          var response = UrlFetchApp.fetch(url,opt);
          data = response;
          return data;

    }

Currently, the data returned from display_basic_data() is

"<html><body>You are being <a href="http://www.website.bla/home">redirected</a>.</body></html>".

If I try to change my script so that "followRedirects" is true, the data is equivalent to the HTML of the login page.

I understand I have to play around with cookies in order to 'stay' logged in but I have no idea what to do as the examples online provided to be fruitless for me.

Any help would be much appreciated!!!

Community
  • 1
  • 1
user3661907
  • 21
  • 1
  • 3

1 Answers1

1

You may want to do something like this:

var cookie = response.getAllHeaders()['Set-Cookie'];
//maybe parse cookies here, depends on what cookie is
var headers = {'Cookie':cookie};
var opt2 = {"headers":headers};
var pagedata = UrlFetchApp.fetch("http://www.website.bla/home",opt2);
akarthik10
  • 322
  • 1
  • 4
  • 11