Javascript 使用Promise时OAuth流不工作

Javascript 使用Promise时OAuth流不工作,javascript,google-chrome-extension,oauth,promise,Javascript,Google Chrome Extension,Oauth,Promise,我正在构建一个简单的chrome扩展,它使用OAuth与Twitter集成。我已经稍微修改了,以便与Twitter集成。扩展是在Reactjs+Flux中构建的 当用户单击“使用Twitter登录”按钮时,将触发操作登录,声明如下: signin: function(){ ChromeUtils.connecttotwitter().then(alert("Step After Then")); AppDispatcher.dispatch({actionType:

我正在构建一个简单的chrome扩展,它使用OAuth与Twitter集成。我已经稍微修改了,以便与Twitter集成。扩展是在Reactjs+Flux中构建的

当用户单击“使用Twitter登录”按钮时,将触发操作登录,声明如下:

signin: function(){
    ChromeUtils.connecttotwitter().then(alert("Step After Then"));       
    AppDispatcher.dispatch({actionType:TweetSmartActions.SIGN_IN,signedInTwitterUserId: response.user_id});
},
var ChromeUtils = {

    connecttotwitter: function () {

        return new Promise(function(fulfill,reject){
            var request = {
                type : "background.twitterRequestToken",
            };
            alert("Before sendMessage");
            chrome.runtime.sendMessage(request, function(response) {

                if (response)
                {
                    fulfill(response);
                }
                else
                {
                    reject(response);
                }
            });
        });
    },
ChromeUtils.connectToWitter()的定义如下:

signin: function(){
    ChromeUtils.connecttotwitter().then(alert("Step After Then"));       
    AppDispatcher.dispatch({actionType:TweetSmartActions.SIGN_IN,signedInTwitterUserId: response.user_id});
},
var ChromeUtils = {

    connecttotwitter: function () {

        return new Promise(function(fulfill,reject){
            var request = {
                type : "background.twitterRequestToken",
            };
            alert("Before sendMessage");
            chrome.runtime.sendMessage(request, function(response) {

                if (response)
                {
                    fulfill(response);
                }
                else
                {
                    reject(response);
                }
            });
        });
    },
事件侦听器onMessage在background.js中定义为:

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {

    console.log("background.js: " + JSON.stringify(request));

    var type = request.type;


    if (type == "background.twitterRequestToken")
    {
        oauth.authorize(function(token,secret,userId,screenname){
            sendResponse({success:true,userId:userId,screenName:screenname});
        });
        alert("Alerting before returning true");
        return true;
    }
当我单击“使用Twitter登录”按钮时,身份验证流确实会启动,并打开一个新页面。然而,在我介绍了Promise之后,新页面并没有重定向到twitter oauth页面。事实上,为了进行调试,我在chrome_ex_oauth.js中添加了以下警报语句:

ChromeExOAuth.prototype.initOAuthFlow = function(callback) {
  if (!this.hasToken()) {
    var params = ChromeExOAuth.getQueryStringParams();
    if (params['chromeexoauthcallback'] == 'true') {
      var oauth_token = params['oauth_token'];
      var oauth_verifier = params['oauth_verifier']
      this.getAccessToken(oauth_token, oauth_verifier, callback);
    } else {
      var request_params = {
        'url_callback_param' : 'chromeexoauthcallback'
      }
      this.getRequestToken(function(url) {
        alert("Url after get request token " + url);
        window.location.href = url;
        alert(window.location.href);
      }, request_params);
    }
这里,第一个警报中的url是twitter oauth url,但第二个警报给出了Chrome扩展url-

chrome-extension://kiekipigbdldhggmlohbnhofnjhcbmem/chrome_ex_oauth.html
为什么没有将url分配给window.location.href


关于可能发生的事情有什么想法吗?

问题不是因为我使用了承诺,而是因为在使用Flux时,在收到承诺的响应之前,动作被调度,这导致应用程序以某种方式挂起

signin: function(){
    ChromeUtils.connecttotwitter().then(alert("Step After Then"));       
    AppDispatcher.dispatch({actionType:TweetSmartActions.SIGN_IN,signedInTwitterUserId: response.user_id});
},

在上面的示例中,应在当时调用的函数中调用AppDispatcher.dispatch

问题并不是因为我使用了承诺,而是因为在使用Flux时,在收到承诺的响应之前,操作被调度,这导致应用程序以某种方式挂起

signin: function(){
    ChromeUtils.connecttotwitter().then(alert("Step After Then"));       
    AppDispatcher.dispatch({actionType:TweetSmartActions.SIGN_IN,signedInTwitterUserId: response.user_id});
},

在上面的示例中,应在当时调用的函数中调用AppDispatcher.dispatch

.then()
接受一个函数对象,而
登录中的
警报(“…”)
是一个函数调用,应该类似于
。然后(alert.bind(null,“…”)
啊,对其进行了更改并进行了测试,但执行从未达到。它挂起是因为window.location.href似乎没有被分配。不,它在后台运行,并在清单中加载如下“后台”:{“scripts”:[“chrome_ex_oauth.js”,“chrome_ex_oauthsimple.js”,“background.js”],所以
chrome\u ex\u oauth.js
也是一个后台脚本。
窗口
对象指的是后台页面窗口,而不是活动的选项卡窗口。你应该使用
chrome.tabs.update
方法或
chrome.tabs.executeScript
来修改活动的选项卡url。我会尝试一下。但是它的作用是因为这是从谷歌在扩展中为Oauth提供了一个示例,在我介绍承诺之前,该页面使用与window.location.href相同的代码访问twitter Oauth url。但让我试试你的建议。
。然后()
接受函数对象,而
警报(“…”)
在您的
登录中:
是一个函数调用,应该类似于
。然后(alert.bind(null,…)
啊,对其进行了更改并进行了测试,但执行从未达到。它挂起,因为window.location.href似乎没有被分配。否,它在后台运行,并在清单中加载,如下“background”:{“scripts”:[“chrome_ex_oauth.js”、“chrome_ex_oauthsimple.js”、“background.js”],所以
chrome\u ex\u oauth.js
也是一个后台脚本。
窗口
对象指的是后台页面窗口,而不是活动的选项卡窗口。你应该使用
chrome.tabs.update
方法或
chrome.tabs.executeScript
来修改活动的选项卡url。我会尝试一下。但是它的作用是因为这是从Google在扩展中为Oauth提供了示例,在我介绍promise之前,该页面使用与window.location.href相同的代码访问twitter Oauth url。但让我试试你的建议。