0

如何在其他网站 (www.sharelatex.com) 上编写 GreaseMonkey 用户脚本以将我带到授权/登录窗口?我的理解是 GM_xmlhttpRequest 将执行此功能,虽然在 GM_xmlhttpRequest 成功加载后我得到“OK”状态,但没有出现登录窗口。我应该使用不同的功能吗?此登录必须以编程方式完成,以便用户脚本可以“捕获”成功登录后附加到重定向 URL 的令牌号。然后,此令牌号将用于 Mendeley API 调用,以从我的 Mendeley 帐户下载所需文件(使用隐式授权流)。

背景信息:我正在尝试构建一个 GreaseMonkey 用户脚本,它将向我的 www.sharelatex.com 帐户添加一个按钮,当按下该按钮时,将使用该站点的 API 从我在 www.mendeley.com 的帐户中自动下载一个文件。与按钮关联的代码还应满足使用 API 所需的登录和身份验证要求。我已经在 Mendeley 注册了我的应用程序,收到了一个“客户 ID”号(0000,用于说明),我用它来构建以下 url:

var urlAuth  = "https://api.mendeley.com/oauth/authorize?client_id=0000&redirect_uri=http://localhost&response_type=token&scope=all"

如果我将上述 URL 作为 URL 地址直接手动输入到浏览器中,我将进入如下所示的登录/授权页面,这正是我想要看到的,但以编程方式而不是手动方式: 单击此处查看身份验证/登录窗口 以下是我的故障 GreaseMonkey 用户脚本的相关位:

// ==UserScript==
//  ... skipping over irrelevant lines ... 
// @grant       GM_xmlhttpRequest
// ==/UserScript==
var urlAuth  = "https://api.mendeley.com/oauth/authorize?client_id=0000&redirect_uri=http://localhost&response_type=token&scope=all"
/* ***************   CREATE BUTTON ************************* */
var input = document.createElement("input"); 
input.type = "button"; 
input.value="Update bibtex"; 
input.onclick = getBib; 
input.setAttribute("style", "font-size:18px; position:absolute; bottom:10px;left:10px;");
document.body.appendChild(input);
/* ================================================================ */
function getBib()
{    
GM_xmlhttpRequest({ 
method: 'GET',
url: urlAuth, 
onload: function(reply) { alert(reply.statusText) }
}

警报指示正常状态,但不会出现登录窗口。当我做一个:

alert(urlAuth)

在 onload 部分中,我手动将警告框中显示的内容复制/粘贴到浏览器地址区域中,浏览器将我带到相应的登录/授权窗口,因此 URL 本身很好。

为什么 GM_xmlhttpRequest 没有将我带到登录屏幕?我是否误解了 GM_xmlhttpRequest 的功能,而应该使用不同的功能?我花了大约 2 个月的时间试图弄清楚这一点,仔细研究了关于 OAuth2、用户脚本、Mendeley API 等主题的数百个参考资料。一些例子:http ://userscripts-mirror.org/scripts/review/ 292038(很有希望,因为它是唯一的 GreaseMonkey/Mendeley 用户脚本,但不幸的是不执行 OAuth2 的东西), https: //salesforce.stackexchange.com/questions/76397/accessing-salesforce-rest-api-through-greasemonkey-脚本(提供的答案从未解决如何获取登录窗口的问题)。

4

1 回答 1

0

虽然 OAuth2 过程的主题涉及多个步骤,但这个问题集中在一个步骤上:如何让授权/登录窗口通过 GreaseMonkey 用户脚本呈现自己。答案(参见上面 Brock Adams 的评论)由meta.stackexchange.com/a/293498/148310中提供的示例给出。更具体地说,登录窗口是由 window.open 函数生成的,如下例所示(参见下面的第 21 行):

// ==UserScript==
<other needed info in the metadata block should be included here>
// @match       https://stackexchange.com/oauth/login_success*
// ==/UserScript==

var rootUrl = "https://api.mendeley.com/oauth/authorize?"
// the rootUrl should point to whatever API service you are trying to use. 
// I am using Mendeley, but could be i.e, facebook, YouTube, Twitter, Google, etc. 
var clientId = "0000"   // needs to be the number you got when you registered your app
// through whatever API service you want to use
var redirectUrl = "https://stackexchange.com/oauth/login_success"  
// The above directs where the login page will be redirected after successful
//     log-in/authorization. 
// This URL needs to point to a real page. 
// Also make sure that whatever is given for the redirectUrl is also 
//     listed in the @match statement in the metadata block section at the top, 
//     but with an astericks at the end (to allow for the token number to be 
//     attached as a hash fragment, as part of the OAuth2 process)
var other   = "response_type=token&scope=all" 
var urlAuth = rootUrl + clientId + "&" + redirectUrl + "&" + other
authWindow = window.open ( urlAuth, "Log in", "resizeable, scrollbars, status, toolbar, 
             dependent, width=660,height=480" ) 
// tailor window sizing, etc. to your own aesthetics
于 2017-10-07T21:01:51.697 回答