如何使用带有凭据的 UrlFetchApp?谷歌脚本 [英] How to use UrlFetchApp with credentials? Google Scripts

查看:15
本文介绍了如何使用带有凭据的 UrlFetchApp?谷歌脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Google Scripts UrlFetchApp 访问具有基本用户名和密码的网站.一旦我连接到该站点,就会出现一个需要身份验证的弹出窗口.我知道登录名和密码,但我不知道如何在 UrlFetchApp 中传递它们.

var response = UrlFetchApp.fetch("htp://00.000.000.000:0000/");Logger.log(response.getContentText("UTF-8"));

当前运行该代码返回拒绝访问".出于安全原因,上面的代码不包含我要连接的实际地址.代码示例中的所有http"都缺少一个t",因为它们被检测为链接并且 Stackoverflow 不允许我提交两个以上的链接.

如何将登录名和密码与我的请求一起传递?无论如何,我可以在登录后继续我的会话吗?或者我的下一个 UrlFetchApp 请求是否会从另一台谷歌服务器发送,要求我再次登录?

这里的目标是登录到 Google 网络基础设施背后的网站,以便它可以充当代理,然后我需要向同一个地址发出另一个 UrlFetchApp 请求,看起来像这样:

var response = UrlFetchApp.fetch("htp://00.000.000.000:0000/vuze/rpc?json={"method":"torrent-add","arguments":{"filename":"htp://vodo.net/media/torrents/anything.torrent","download-dir":"C:\temp"}} ");Logger.log(response.getContentText("UTF-8"));

解决方案

此问题已在其他地方得到解答.总结如下:

布鲁斯·麦克弗森

<块引用>

基本认证看起来像这样...var 选项 = {};options.headers = {"Authorization": "Basic " + Utilities.base64Encode(username + ":" + password)};

莱尼·坎宁安

<块引用>

//添加基本授权/////////////////////////////////////////////////////////////////////////////////////////var USERNAME = PropertiesService.getScriptProperties().getProperty('username');var PASSWORD = PropertiesService.getScriptProperties().getProperty('password');var url = PropertiesService.getScriptProperties().getProperty('url');////////////////////////转发

WebRelay 的端口

 var headers = {授权":基本"+ Utilities.base64Encode(用户名+':'+密码)};变量参数 = {"方法":"GET",标题":标题};var 响应 = UrlFetchApp.fetch(url, params);

I am trying to use Google Scripts UrlFetchApp to access a website with a basic username and password. As soon as I connect to the site a popup appears that requires authentication. I know the Login and Password, however I do not know how to pass them within the UrlFetchApp.

var response = UrlFetchApp.fetch("htp://00.000.000.000:0000/‎");   
Logger.log(response.getContentText("UTF-8"));

Currently running that code returns "Access Denied". The above code does not contain the actual address I am connecting to for security reasons. A "t" is missing from all the "http" in the code examples because they are being detected as links and Stackoverflow does not allow me to submit more than two links.

How can I pass the Login and Password along with my request? Also is there anyway I can continue my session once I have logged in? Or will my next UrlFetchApp request be sent from another Google server requiring me to login again?

The goal here is to login to the website behind Googles network infrastructure so it can act as a proxy then I need to issue another UrlFetchApp request to the same address that would look something like this:

var response = UrlFetchApp.fetch("htp://00.000.000.000:0000/vuze/rpc?json={"method":"torrent-add","arguments":{"filename":"htp://vodo.net/media/torrents/anything.torrent","download-dir":"C:\temp"}}‎");   
Logger.log(response.getContentText("UTF-8"));

解决方案

This question has been answered on another else where. Here is the summary:

Bruce Mcpherson

basic authentication looks like this...
    var options = {};
    options.headers = {"Authorization": "Basic " + Utilities.base64Encode(username + ":" + password)};

Lenny Cunningham

//Added Basic Authorization//////////////////////////////////////////////////////////////////////////////////////////

  var USERNAME = PropertiesService.getScriptProperties().getProperty('username');
  var PASSWORD = PropertiesService.getScriptProperties().getProperty('password');

  var url = PropertiesService.getScriptProperties().getProperty('url');//////////////////////////Forwarded

Ports to WebRelay

  var headers = {
    "Authorization" : "Basic " + Utilities.base64Encode(USERNAME + ':' + PASSWORD)
  };

  var params = {
    "method":"GET",
    "headers":headers
  };

var reponse = UrlFetchApp.fetch(url, params);

这篇关于如何使用带有凭据的 UrlFetchApp?谷歌脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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