且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用GreaseMonkey用户脚本以编程方式打开登录窗口以进行隐式授权

更新时间:2023-12-04 10:30:58

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

While the topic of OAuth2 process involves multiple steps, this question was focused on one step: how to get the authorization / log-in window to present itself through a GreaseMonkey userscript. The answer (see comment by Brock Adams above) is given by an example provided in meta.stackexchange.com/a/293498/148310. More specifically, the log-in window is produced by the window.open function, as illustrated in the following example (see Line #21 below):

// ==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, ***, 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