2016-04-18 37 views
4

我正尝试使用谷歌日历范围向客户端javascript请求谷歌oauth端点(https://accounts.google.com/o/oauth2/v2/auth) 。事情是,我不能使用谷歌的JavaScript客户端,因为我实际上使用webpack,而且我不想单独在我的bundle.js之外包含JavaScript脚本。Google oauth 400响应:请求的资源上没有“Access-Control-Allow-Origin”标头

所以相反,我使用axios(https://github.com/mzabriskie/axios)将HTTP GET设置为上述的令牌端点。以下是我的请求的样子:

https://accounts.google.com/o/oauth2/v2/auth?response_type=token&client_id={client id here}&nonce=c8ef445540186351d9108ad64d7a5b65&scope=https:%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar 

我使用crypto-js库的MD5函数生成了随机数。下面是请求头:

Accept:application/json, text/plain, */* 
Origin:http://localhost:8000 
Referer:http://localhost:8000/admin 
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) 
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36 

我从谷歌得到的回应是这样的:

alt-svc:quic=":443"; ma=2592000; v="32,31,30,29,28,27,26,25" 
alternate-protocol:443:quic 
cache-control:no-cache, no-store, max-age=0, must-revalidate 
content-encoding:gzip 
content-type:text/html; charset=utf-8 
date:Mon, 18 Apr 2016 07:16:21 GMT 
expires:Fri, 01 Jan 1990 00:00:00 GMT 
pragma:no-cache 
server:GSE 
status:400 
x-content-type-options:nosniff 
x-frame-options:SAMEORIGIN 
x-xss-protection:1; mode=block 

,我看到我的铬devtools控制台此日志:

XMLHttpRequest cannot load https://accounts.google.com/o/oauth2/v2/auth?response_type=token&client_id={client id here}&scope=https:%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access. The response had HTTP status code 400. 

我我确信在我的谷歌应用程序控制台中,在相应的客户端ID下,我已将http://localhost:8000添加到“授权的Javascript起源”字段中,并且我已确保为谷歌日历启用授权api。我读过网络客户端隐式认证流程不使用重定向uri,但我已经尝试填补了这一点,不管(我已经尝试了值http://localhost:8000http://localhost:8000/admin,这是我发送我的请求的页面)。花了数小时搜索这个,我发现没有什么可以表明我在做什么不应该工作。

所以我的问题是,如果我允许http://localhost:8000作为我的客户端ID的授权来源,为什么我仍然无法发送请求(尽管通过javascript)到该auth端点?

+0

我相信你应该** **重定向浏览器到这的oauth2终点,而不是只是试图让一个Ajax请求它。 https://developers.google.com/identity/protocols/OAuth2UserAgent#overview –

+0

@WiktorZychla:出于某种原因,我的印象是谷歌会给我一个重定向响应,但后来我意识到这会是多么愚蠢,考虑到我正在提出CORS请求。这现在更有意义了,谢谢! –

+0

如果这个评论适合你,我会把它作为答案发布。 –

回答

相关问题