launchWebAuthFlow 回调未运行,Chrome 扩展程序窗口立即关闭?

srl*_*020 6 javascript google-chrome-extension

我正在使用 chrome.identity.launchWebAuthFlow 从我的 Rails 应用程序中检索身份验证代码,该应用程序使用 Doorkeeper gem 设置为 OAuth2 提供程序(Doorkeeper 方面正在工作)。

因此,我使用 Chrome 扩展程序中的此方法将请求发送到我的服务器:

requestGrant: function(){

    chrome.identity.launchWebAuthFlow(
      {
        'url': authService.grantEndPoint(),
        'interactive': true
      }, function(redirect_url){

    /* Extract auth code from redirect_url */ 
      var code = authService.extractCode(redirect_url);
      alert(code);
      authService.getAccessToken(code);
    }); //launchWebAuthFlow ends here

  }
Run Code Online (Sandbox Code Playgroud)

我的服务器收到请求并重定向到

https://<my_chrome_extension_name>.chromiumapp.org/oauth2?code=<access_code_generated_by_oauth>
Run Code Online (Sandbox Code Playgroud)

发现 302。

但是,Chrome 扩展立即关闭,并且launchWebAuthFlow 的回调函数没有运行。我知道它没有运行,因为我在回调中调用了 alert()(未运行),并向我的服务器发出另一个请求以获取访问令牌(服务器未收到请求)。

我认为部分问题在于,当调用 launchWebAuthFlow 时,chrome 扩展可能会关闭,因为 launchWebAuthFlow 打开的窗口是服务器授权流的 Web 视图,并且扩展关闭(或者只是通过延期?)

出于某种原因,launchWebAuthFlow 的默认行为是根据文档关闭窗口,但回调仍未运行。

如何让回调函数运行,并防止 chrome 扩展窗口关闭?

srl*_*020 6

我在非后台脚本中运行 launchWebAuthFlow。我将身份验证逻辑移至后台脚本,并且工作正常。