1

I need to implement a Google Apps Script app that is used to login on a website and then if the authentication process succeed on that website the user should receive a message in the google script sidebar.

For example: the user enters his email and password and then he press the Login button, then he should be logged in on the website if the credentials are correct.

Let me know if I need to provide more details on this... I am new with Google Apps Script and I really need some help with this login process. Thank you!

I tried to implement the following code but I receive the following error message when executing the login function:

Request failed for https://example.com/login returned code 405.

HTML file:

<div class="form-auth">
  <label class="inline">username</label>
  <input type="text" placeholder="Insert Email"/>
</div>
<div class="form-auth">
  <label class="inline">password</label>
  <input placeholder="Insert Password"/>
</div>
<button class="btn-default">Login</button>

Google Script file:

function onOpen() {
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
      .createMenu('Menu')
      .addItem('Show sidebar', 'showSidebar')
      .addToUi();
}

function showSidebar() {
  var html = HtmlService.createHtmlOutputFromFile('login')
      .setSandboxMode(HtmlService.SandboxMode.IFRAME)
      .setTitle('SDR Tag Governance')
      .setWidth(300);
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
      .showSidebar(html);
}

function login() {
  var payload =
   {
     "username" : "[email protected]",
     "password" : "myPassword",
   };
  var options =
   {
     "method" : "post",
     "payload" : payload,
     "followRedirects" : false
   };
  var login = UrlFetchApp.fetch("https://example.com/login" , options);
  var sessionDetails = login.getAllHeaders()['Set-Cookie'];
}
2
  • Status 405 means method not allowed, Maybe your url does not require what you are sending it. Did you try doing the same login call from browser using ajax? While doing from browser console be sure you are on the same domain i.e example.com unless you have CORS disabled. Commented Jun 8, 2016 at 5:28
  • I managed to solve this by adding the headers option. Commented Jun 8, 2016 at 9:13

1 Answer 1

2

Fixed this by adding headers option:

  var headers = {
    'Connection':'keep-alive',
    'Content-Type':'application/json;charset=utf-8',
    'Accept':'application/json, text/plain, */*',
    'Cookie':'...',
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.