Robert Goth
Robert Goth

Reputation: 85

Set multiple cookies in google app script with UrlFetchApp

I am using the following script to access a web page that requires user to be logged-in. The script works up to the point where I retrieve the login cookies I need help with using the cookies in the last part of the following code.

function login(strUsername, strPassword) {

  //request Page
  var loginPage = UrlFetchApp.fetch("https://login.url.org/");

  //strip out "authState" value from response
  var sessionDetails = loginPage.getContentText()
  var searchString = sessionDetails.indexOf('"authState":"');
  var newStringRight =  sessionDetails.substring(searchString+13, searchString+6000);
  var authState =  newStringRight.substring(0, newStringRight.indexOf('"'));

  //Logger.log(authState);

//set payload 
  var payload =
   {"authState":authState,
    "username":strUsername,
    "password":strPassword
   };


  var options =
   {
     "method" : "post",
     "payload" :  JSON.stringify(payload),
     "followRedirects" : false,
      muteHttpExceptions: true,
      "contentType": "application/json"
   };

    //Logger.log(options);
   var loginResponse = UrlFetchApp.fetch("https://login.url.com/api/authenticate/credentials", options);


  var loginHtml = loginResponse.getContentText();
  Logger.log(loginHtml); 
  // Log has the following response

  //{"output":{"method":"SUCCESS","cookies":{"ObSSOCookie":"xxx","ChurchSSO":"yyy"}}}

  if (loginHtml.indexOf("SUCCESS")>0){
     Browser.msgBox('Pass', 'Successfully loged in.', Browser.Buttons.OK); 

     //strip out cookies
     searchStringStart = loginHtml.indexOf('{"ObSSOCookie":"');
     searchStringEnd = loginHtml.indexOf('"}}');
     var Cookie =  loginHtml.substring(searchStringStart, searchStringEnd+2);  
     Logger.log(Cookie); 
      /*
       Logger.log(cookie) returns
       {"ObSSOCookie":"xxxx","ChurchSSO":"yyyy"}
     */

////////////////////////////Works up to here/////////////////////////////////////////    
     var options =
         {
       "headers" : {"cookies" : Cookie},
        muteHttpExceptions: true,
        "contentType": "application/json"
     };

    var adminpage= UrlFetchApp.fetch("https://url.com/admin",options ); 
    Logger.log(adminpage);
   /*
             <h1>Access Denied</h1>
            <p>You are not authorized to access this page.</p>
            <p><a href="javascript:location.reload()">Retry</a> 
   */


   }else{
     Browser.msgBox('Fail', 'Sign in failed. Please try again.', Browser.Buttons.OK);  
   }

}

I think it may have to do with the fact that I am trying to use multiple cookies {"ObSSOCookie":"xxxx","ChurchSSO":"yyyy"}not sure how to approperatly pass them into the new page call. Any help would be great

Upvotes: 1

Views: 1649

Answers (1)

TheMaster
TheMaster

Reputation: 50445

Cookies should be sent inside a single Cookie header separated by a ;(semicolon and a space):

Cookie: name1=value1; name2=value2; 

Snippet:

var options =
     {
       "headers" : {"Cookie" : "ObSSOCookie=xxxx; ChurchSSO=yyyy"}},
     };

Reference:

Upvotes: 2

Related Questions