1

私は javascript.for を使用して oAuth を実行しています。このために、認証 URL を使用して新しいウィンドウを開き、この新しいウィンドウでユーザーが認証され、ウィンドウに access_token を持つページが読み込まれます。

ここで、元のウィンドウからこのトークンにアクセスしたいので、基本的には、ユーザーが承認し、開いたウィンドウに新しいページ (access_token を使用) が読み込まれるまで待ってから、アクセスします。これを行う方法

authWin=window.open('https://login.salesforce.com/services/oauth2/authorize?   response_type=token&client_id=3MVG9Y6d_Btp4xp4VNFwdCLxJdMbnQrg44lVcvrLG5Qjp3serQQunkCgdS1bYXMSYDQhiSWazvF_J7oVZ9dBw&redirect_uri=sfdc://success','','width=500,height=300');

//code which is not working
while(true){
    var uri = authWin.document.URL;
    if(uri.indexOf('AuthorizationPage')!=-1){
       alert('now I can get access_token');
       break;
    }
 }
 //I know while(true) locks the system,but please provide alternative for this

助けてください

4

1 に答える 1

0

setInterval() を使用してこれを行うことができます

var timerId = setInterval(function () {

            if (typeof(authWin.opener) === 'object') { //if user closed authWin stop awaiting auth
                try {
                    var tokenUri = authWin.location.pathname; //this throwing a error while user not on the same domain(see "same origin policy"), we catch exception and processing like awaiting user back
                    clearInterval(timerId); //when user get back after remote auth on our domain we again have access to authWin properties

                    var token = tokenUri.replace(/\/#access_token=(.+?)&.+/, '$1'); //parsing token from uri
                    authService.saveAuthToken(token);
                    authWin.close();

                    //do something ...
                }
                catch (err) {
                    console.log('waiting for user back');
                }
            }
            else {
                clearInterval(timerId);
            }

        }, 2000);
于 2015-12-23T08:22:49.117 に答える