问题描述
我需要实现一个用于在网站上登录的谷歌脚本应用程序,然后如果认证过程在该网站上成功,用户应该在谷歌脚本边栏中收到一条消息。
I need to implement a google 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 app script and I really need some help with this login process. Thank you!
我试着实现下面的代码,但是当执行 login
函数时收到以下错误消息: https://example.com/login的请求失败返回代码405。
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文件:
<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脚本文件:
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" : "myEmail@domain.com",
"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'];
}
推荐答案
:
Fixed this by adding headers option:
var headers = {
'Connection':'keep-alive',
'Content-Type':'application/json;charset=utf-8',
'Accept':'application/json, text/plain, */*',
'Cookie':'...',
}
这篇关于Google脚本在外部网站上进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!