2017-10-05 3 views
0

GreaseMonkeyのuserscriptをコード化して、別のWebサイト(www.sharelatex.com)で私を認可/ログインウィンドウに連れて行く方法を教えてください。私の理解は、GM_xmlhttpRequestがこの機能を実行し、GM_xmlhttpRequestが正常にロードされた後に「OK」ステータスを取得している間は、ログインウィンドウが表示されません。私は別の機能を使用すべきですか?このログインは、ログインが成功した後にリダイレクトURLに添付されるトークン番号を「キャッチ」できるように、プログラムで行わなければなりません。このトークン番号は、Mendeley API呼び出しで使用され、(暗黙の許可フローを使用して)Mendeleyアカウントから目的のファイルをダウンロードします。GreaseMonkey userscriptを使用してログインウィンドウをプログラムで暗黙的に許可する方法

背景情報:www.sharelatex.comアカウントにボタンを追加するGreaseMonkeyのユーザースクリプトを作成しようとしています。プッシュするとwww.mendeley.com上の自分のアカウントからそのサイトのファイルを自動的にダウンロードしますAPI。ボタンに関連付けられたコードには、APIを使用するために必要なログインと認証の要件も必要です。私は手動に直接上記のURLを入力した場合

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

:私はMendeleyと自分のアプリケーションを登録して、私は次のURLを構築するために使用している(説明の目的のために0000、)「クライアントID」番号を受け取りました私のブラウザはURLアドレスとして、以下のようなログイン/認証ページに行きます。これは私が見たいものですが、手動ではなくプログラムで行います: Click here to view authentication/login window 以下は、誤ったGreaseMonkey userscript :

// ==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) } 
} 

警告アラートOKステータスですが、ログインウィンドウは表示されません。私がないとき:onloadイベントセクションおよびI内

alert(urlAuth) 

手動/ブラウザのアドレス領域に警告ボックスに表示される内容コピー&ペースト、ブラウザはそのURL自体、適切なログイン/認証ウィンドウに私を取ります結構です。

GM_xmlhttpRequestがログイン画面に移動しないのはなぜですか?私はGM_xmlhttpRequestの機能を誤解していますか?代わりに別の機能を使用する必要がありますか?私はOAuth2、userscripts、Mendeley APIなどのトピックに関する何百もの参考文献を参照しながら、これを理解しようと約2ヶ月を費やしました。いくつかの例:http://userscripts-mirror.org/scripts/review/292038(これは唯一のGreaseMonkey/Mendeleyユーザースクリプトですので有望でした残念ながらOAuth2を実行しません)、 https://salesforce.stackexchange.com/questions/76397/accessing-salesforce-rest-api-through-greasemonkey-script(提供された回答では、ログインウィンドウの取得方法の問題は解決されませんでした)

+0

答えは 'window.open'と' postMessage'です。例については、https://meta.stackexchange.com/a/293498/148310を参照してください。 –

+0

返信いただきありがとうございます、@ BrockAdams。私はのサンプルコードをほぼそのまま入力しており、多くのフォローアップの質問があります(私は新しい、別々の投稿で尋ねる)。ログインするためのポップアップウィンドウを取得するには、ここで行ったことがあります(明示的に他の人にも効果があります): 'authWindow = window。urlAuthが上記で定義されているところで、 urlAuth、 "ログイン"、 "サイズ変更可能、スクロールバー、ステータス、ツールバー、依存、幅= 660、高さ= 480") ' – PMM

+0

@BrockAdams、チャンスがあれば、私がここに投稿したフォローアップの質問を見ていただければ幸いです。https://stackoverflow.com/questions/46615950/my-redirect-uri-results-in-an-unable接続警告です。ここでの質問は満足のいくものだと私は考えます。それが私が新しい質問/ポストを開いた理由です。 – PMM

答えて

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 
関連する問題