使用堆栈应用程序 oauth 2.0 的 chrome 扩展中的错误 "Cannot return to provided redirect_uri"

error "Cannot return to provided redirect_uri" in chrome extension using stackapp auth 2.0

我按照此处的教程进行操作 https://developer.chrome.com/apps/app_identity and use the api here https://developer.chrome.com/apps/identity,但没有成功。谁能指出这段代码有什么问题吗?

function onGoogleLibraryLoaded() {
    var redirect_uri = chrome.identity.getRedirectURL("http://qqibrow.github.io");
    var full_url =  "https://stackexchange.com/oauth/dialog?client_id=4716&redirect_uri=" + redirect_uri;
    console.log(redirect_uri);
    console.log(full_url);
    chrome.identity.launchWebAuthFlow({
        'url': full_url,
        'interactive': true
    }, authorizationCallback);
}

var authorizationCallback = function (data) {
    // should print out redirect_uri with auth_token if succeed.
    console.log(data);
};

// manifest.json
// ...
"permissions": [
    "activeTab",
     "identity",
    "https://ajax.googleapis.com/",
      "https://stackexchange.com/*",
        "https://stackexchange.com/oauth/*",
        "http://qqibrow.github.io/*"
  ],
   "web_accessible_resources": [
        "http://qqibrow.github.io/*",
        "https://stackexchange.com/*",
  ],
  // ... 

如果我尝试 https://stackexchange.com/oauth/dialog?client_id=4716&redirect_uri=http://qqibrow.github.io 它确实有效。但是使用上面的代码,我总是从 stackexchange 得到一个错误页面,说:

Application Login Failure error description: Cannot return to provided redirect_uri.

这是 chrome.identity.getRedirectURL() 的创意用法。

它不允许您重定向到任意域;您可以提供路径,但 chrome.identity 的域将为 https://<app-id>.chromiumapp.org.

因此,您的调用 returns https://<app-id>.chromiumapp.org/http://qqibrow.github.io 无效 URL,并且您的身份验证失败。

我建议重新阅读 launchWebAuthFlow documentation