我正在开发一个单页应用程序,该应用程序也需要离线访问 Microsoft OneDrive。因此,我指导用户使用代码流方法通过 OneDrive 进行身份验证。
应用程序已成功检索代码,但是当调用 https://login.live.com/oauth20_token.srf 时请求因 CORS 错误而失败。
请参阅我在 Chrome 上的 javascript 控制台中获取 HTTP header 的附图。服务器的响应缺少 header
Access-Control-Allow-Origin:
导致 CORS 错误
如何为我的应用启用 CORS 访问?我在开发者控制台中没有看到任何选项?
谢谢!
最佳答案
收到 access_token 后,您应该能够使用 CORS 向 OneDrive API 发出经过身份验证的请求。从提供的 HTTP header 来看,您似乎需要再执行一次调用来兑换访问 token 的代码。您可能想查看http://onedrive.github.io/auth/msa_oauth.htm (第2步)。收到访问 token 后,您需要将访问 token 添加到请求的“授权” header 中。下面是使用 CORS 和响应 header 的 HTTP 请求示例。我希望这会有所帮助。
var xhr = new XMLHttpRequest();
var oauthAccessToken = accessTokenStoredHere();
xhr.open('GET',
'https://api.onedrive.com/v1.0/drive/');
xhr.setRequestHeader('Authorization',
'Bearer ' + oauthAccessToken);
xhr.send();
GET /v1.0/drive/
HTTP/1.1
Authorization:Bearer YOUR_ACCESS_TOKEN
Host:api.onedrive.com
X-Target-URI:https://api.onedrive.com
Connection:Keep-Alive
关于OneDrive oAuth token 请求收到 CORS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29425390/