Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取Google API访问令牌_Javascript_Ajax - Fatal编程技术网

Javascript 获取Google API访问令牌

Javascript 获取Google API访问令牌,javascript,ajax,Javascript,Ajax,我正在编写一个JS应用程序,用于从谷歌日历进行读写。我不能直接使用GoogleAPI,因为它与我的目标环境不兼容,它在本地Apache服务器上作为单个网页运行。为此,我使用的是XmlHttpRequest,这很好,但只有在我包含一个访问令牌ie时才起作用 xmlReq.setRequestHeader("Authorization", "Bearer " + GOOGLE_ACCESS_TOKEN); 所以我要做的是使用一个初始的XHR来获取访问令牌,我一直在遵循这个文档 这将返回一个错

我正在编写一个JS应用程序,用于从谷歌日历进行读写。我不能直接使用GoogleAPI,因为它与我的目标环境不兼容,它在本地Apache服务器上作为单个网页运行。为此,我使用的是XmlHttpRequest,这很好,但只有在我包含一个访问令牌ie时才起作用

   xmlReq.setRequestHeader("Authorization", "Bearer " + GOOGLE_ACCESS_TOKEN);
所以我要做的是使用一个初始的XHR来获取访问令牌,我一直在遵循这个文档 这将返回一个错误消息:

“请求的资源上不存在“Access Control Allow Origin”标头。因此不允许Origin localhost访问。”

我已经看到这条消息与XHR跨域请求相关,但我看不出这里有什么问题

有什么想法吗

代码如下:

     this.xmlReq = new XMLHttpRequest();
     this.currentUrl='https://accounts.google.com/o/oauth2/auth?'; 
     this.currentUrl += 'response_type=token';
     this.currentUrl += '&redirect_uri=http://localhost';
     this.currentUrl += '&client_id=999999999999.apps.googleusercontent.com';
     this.currentUrl += '&scope=https://www.googleapis.com/auth/calendar';
     this.xmlReq.open("GET", this.currentUrl, true);
     this.xmlReq.responseType = "auth"; 
     this.xmlReq.send();

听起来你可能需要看看你的apache配置,特别是你的代理设置,ProxyPass,如果你有一个内部代理服务器,那么就看看ProxyRemote。如果我在相同的环境中使用Google API,它就会工作。即gapi.auth.authorize后跟gapi.auth.getToken。我正在寻找的解决方案是直接使用Javascript。