如何使用Google Apps脚本登录外部网站? [英] How to login to an external website with Google Apps Scripts?

查看:103
本文介绍了如何使用Google Apps脚本登录外部网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试登录网站,以便可以将数据拉回到Google文档电子表格.我在这里阅读过各种文章,但无法弄清楚如何识别需要传递的数据.

I'm trying to login to a website so that I can pull back data to a Google docs spreadsheet. I've read various posts here, but I can't work out how to identify the data I need to pass.

该网站的登录页面具有一个包含以下字段的表单.

The login page of the site has a form with the following fields.

<form action="https://fantasyfootball.telegraph.co.uk/premierleague/log-in/" method="post" id='reg-form'>
        <fieldset class='login'>

            <div class="required">
                <label for="email">Email:</label>
                <input type="text" name="email" id="email" class="input-text" size="10" maxlength="50" value="my.name@address.com" />
            </div>

            <div class="required">
                <label for="pass">Password:</label>
                <input type="password" name="pass" id="pass" class="input-password" size="10" maxlength="15" value="some-password" />
            </div>
        </fieldset>
        <div id="remember-me-container">
                <input type="checkbox" checked="checked" id="remember-me" name="remember-me" value="remember-me" />
                <label for="remember-me" id="remember-lbl">Remember me</label>

        </div>

        <input type="submit" id="submit-btn" value="Login" name='Submit' class='btn'/>
    </form>

我尝试了以下脚本,但是sessionDetails总是以未定义"的形式返回.

I've tried the following script, but the sessionDetails always comes back as "Undefined."

// Returns the html of the page.
function sGetPage (sUrl) {

var url = "https://fantasyfootball.telegraph.co.uk/premierleague/log-in/";

// logging in, following http://stackoverflow.com/questions/21621019/google-apps-script-login-to-website-with-http-request
var payload =
{
 "email" : "ian.shaw@iee.org",
 "pass" : "asdf123",
 "submit-btn": "Login",
 "remember-me" : "remember-me" 
};

var options =
{
 "method" : "post",
 "payload" : payload,
 "followRedirects" : false
};

var login = UrlFetchApp.fetch( url, options);
var sessionDetails = login.getAllHeaders()['Set-Cookie'];
Logger.log(sessionDetails); 

var response = UrlFetchApp.fetch ("https://fantasyfootball.telegraph.co.uk/premierleague/leagues/view/8000912/4015677/", {"headers" : {"Cookie" : sessionDetails} });
var sHtml = response.getContentText();
Logger.log(sHtml); 

}

任何建议将不胜感激.

推荐答案

我从来没有能够将sessionDetails原样发送回站点.我一直在使用RegEx提取sessionDetails的相关部分并创建一个新的cookie字符串,然后将其发送回站点.要找出Cookie的哪些部分是相关的,请使用浏览器的网络日志(在开发人员工具中)检查您的浏览器为Cookie发布的内容,并将该字符串与sessionDetails进行比较.我在此处发布了一个示例.

I haven't ever been able to send sessionDetails back to the site as-is. I've been using RegEx to extract the relevant parts of sessionDetails and create a new cookie string, and send that back to the site. To find out what parts of the cookie are relevant, use your browser's network log (in the developer tools) to examine what your browser posts for the cookie, and compare that string to sessionDetails. I posted an example here.

var login = UrlFetchApp.fetch(url, options);
var sessionDetails = login.getAllHeaders()['Set-Cookie'];
Logger.log(sessionDetails); 
var cookie = sessionDetails.match(/Asp\.NetSessionId=[A-Z0-9]+;/)[0];  //modify this RegEx as needed

var response = UrlFetchApp.fetch(
  "https://example.com",
  {"headers" : {"Cookie" : cookie} }
);

这篇关于如何使用Google Apps脚本登录外部网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆