We’d like to remind Forumites to please avoid political debate on the Forum.

This is to keep it a safe and useful space for MoneySaving discussions. Threads that are – or become – political in nature may be removed in line with the Forum’s rules. Thank you for your understanding.

📨 Have you signed up to the Forum's new Email Digest yet? Get a selection of trending threads sent straight to your inbox daily, weekly or monthly!
The Forum now has a brand new text editor, adding a bunch of handy features to use when creating posts. Read more in our how-to guide

Anyone know Java/Google script?

I'm trying to write a simple bit of script in Google sheets that will log into a website for me and grab the cookie from the header, but it isn't working. Is there anyone that knows the language and what could be wrong?
var payload =
   {
     "user_login" : "free",
     "user_pass" : "password",
   };
  var options =
   {
     "method" : "post",
     "payload" : payload,
     "followRedirects" : false
   };
  var login = UrlFetchApp.fetch("https://example.com/wp-login.php" , options);
  var sessionDetails = login.getAllHeaders()['Set-Cookie'];
Faith, hope, charity, these three; but the greatest of these is charity.

Comments

  • Sooler
    Sooler Posts: 3,114 Forumite
    Part of the Furniture 1,000 Posts Combo Breaker
    Which bit isn't working? The log in or getting the cookie?
  • IronWolf
    IronWolf Posts: 6,462 Forumite
    Part of the Furniture 1,000 Posts Name Dropper Combo Breaker
    edited 5 November 2014 at 11:15AM
    Sooler wrote: »
    Which bit isn't working? The log in or getting the cookie?

    The login

    I found some better code specifically for wordpress, but still not working
    var options = {
          "method": "post",
          "payload": {
          "log": "free",
          "pwd": "password",
          "wp-submit": "Login"
          },
          "followRedirects": false
       };
      var login = UrlFetchApp.fetch("http://example.com/wp-login.php" , options);
       var text = login.getContentText(); 
    Logger.log(text);
      var cookie = login.getAllHeaders()['Set-Cookie'];
      Logger.log(cookie);
    

    The webpage output is just the login form and cookie doesn't exist.

    I've tried it without redirects=false as well and still the same problem.
    Faith, hope, charity, these three; but the greatest of these is charity.
  • fannyadams
    fannyadams Posts: 1,752 Forumite
    Part of the Furniture 1,000 Posts Photogenic Name Dropper
    you're missing a comma but I can't see where exactly.
    just in case you need to know:
    HWTHMBO - He Who Thinks He Must Be Obeyed (gained a promotion, we got Civil Partnered Thank you Steinfeld and Keidan)
    DS#1 - my twenty-five-year old son
    DS#2 - my twenty -one son
  • IronWolf
    IronWolf Posts: 6,462 Forumite
    Part of the Furniture 1,000 Posts Name Dropper Combo Breaker
    fannyadams wrote: »
    you're missing a comma but I can't see where exactly.

    How do you know that's the problem?
    I've looked through and can't see anywhere that a comma is missing :S
    Faith, hope, charity, these three; but the greatest of these is charity.
  • IronWolf
    IronWolf Posts: 6,462 Forumite
    Part of the Furniture 1,000 Posts Name Dropper Combo Breaker
    Nevermind, I found an alternate solution using basic authentication if anyone is interested

    http://stackoverflow.com/questions/23546255/how-to-use-urlfetchapp-with-credentials-google-scripts

    The cookies thing just never seemed to work
    Faith, hope, charity, these three; but the greatest of these is charity.
This discussion has been closed.
Meet your Ambassadors

🚀 Getting Started

Hi new member!

Our Getting Started Guide will help you get the most out of the Forum

Categories

  • All Categories
  • 353.5K Banking & Borrowing
  • 254.2K Reduce Debt & Boost Income
  • 455.1K Spending & Discounts
  • 246.6K Work, Benefits & Business
  • 602.9K Mortgages, Homes & Bills
  • 178.1K Life & Family
  • 260.6K Travel & Transport
  • 1.5M Hobbies & Leisure
  • 16K Discuss & Feedback
  • 37.7K Read-Only Boards

Is this how you want to be seen?

We see you are using a default avatar. It takes only a few seconds to pick a picture.