javascript - Chrome 扩展程序 20 小时后没有 'Access-Control-Allow-Origin' header

标签 javascript google-chrome google-chrome-extension oauth-2.0 cors

我使用 chrome.identity.getAuthToken 与某些内部 API 进行通信。 前 20 小时一切都很完美,我可以调用 API 并获得我需要的东西。

大约 20 小时后,我的扩展停止工作,我得到:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://MY_API' is therefore not allowed access.

我知道 chrome.identity.getAuthToken 将更新过期的 token ,并且我可以在 chrome://identity-internals/ 看到我的代码获得了新的访问权限 token 但无法发送请求。

如果我再次卸载/安装扩展程序,它就会再次工作。

list 包含:

"permissions": [ "tabs", "activeTab", "cookies", "identity", "<all_urls>" ]

当我重新安装扩展程序时会发生什么情况,从而使错误消失?

背景.js:

    chrome.identity.getAuthToken({interactive: true}, function(token) {

        gapi.auth.setToken({'access_token': token});

        // Load the storage API.
        gapi.client.load(API_ROOT + DISCOVERY_PATH,
            VERSION, function() {
          // Initialize GAPI's OAuth.
          gapi.auth.init(function() {
            var request = "It's request to my API";

            // Not working after 20 hours
            request.execute(function(response) {
             // do something
            });

          });
        });
      })

还尝试调用“chrome.identity.removeCachedAuthToken”,但扩展程序在 20 小时内停止工作。

最佳答案

在服务器“https://MY_API”上设置此设置:

Header always append X-Frame-Options SAMEORIGIN
Header add Access-Control-Allow-Origin "*"

在 .htaccess 或 httpd.conf 中(如果服务器是 httpd/apache)

关于javascript - Chrome 扩展程序 20 小时后没有 'Access-Control-Allow-Origin' header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38129633/

相关文章:

javascript - AWS S3 无法访问的主机

javascript - Chrome 中的 Canvas 性能变化

javascript - 可以通过 Chrome 扩展程序修改窗口对象吗?

javascript - 使用 activeTab 权限与 <all_urls>

javascript - Chrome 扩展 : How to get/detect click events on any page or any where. 就像 Google Dictionary Plugin 一样?

javascript - Spring REST 未从 MultiPartHttpServletRequest 获取文件

javascript - 如何在 mvc 和 JavaScript 中使用异步获取发出发布请求?

javascript - IE11 - 对象不支持属性或方法 'next' - Javascript

Javascript 在 Chrome Extension 中并不总是有效,我应该什么时候运行它?

javascript - 谷歌浏览器扩展程序 : Omnibox Keyword How To?