我正在开发一个Spotify应用程序,并且我想获取令牌。客户端凭证流程使用卷曲但不在浏览器中
我下面Client Credentials Flow和使用curl
一切正常:
$ curl -H "Authorization: Basic YjU4Y...llYTQ=" \
-d grant_type=client_credentials \
https://accounts.spotify.com/api/token
# Response:
# {
# "access_token":"BQD3u...W4iJA",
# "token_type":"Bearer",
# "expires_in":3600
# }
在这里,有我的HTML文件的Javascript代码,我试图得到相同的结果:
var url = "https://accounts.spotify.com/api/token";
var authentication = "YjU4Y...llYTQ=";
var params = { grant_type: "client_credentials" };
var auth = "Basic " + authentication;
$.ajax({
url: url,
type: 'POST',
dataType: 'json',
headers: {
'Authorization' : auth,
'Access-Control-Allow-Origin': '*'
},
data: params,
success: function(data) {
console.log('success', data);
}
});
但是,我收到以下错误:
XMLHttpRequest cannot load https://accounts.spotify.com/api/token . Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
什么我做错了吗?
真的有一种方法可以从使用Javascript的静态HTML文件中使用Spotify API吗?
http://stackoverflow.com/questions/33188989/allowing-cors-jquery-post-requests-to-spotify-api-on-express-js-server/33198424#33198424似乎相关。和https://github.com/spotify/web-api-auth-examples。来自Spotify的http://stackoverflow.com/users/540274/jos%C3%A9-m-p%C3%A9rez似乎围绕SO回答问题。 – sideshowbarker
谢谢@sideshowbarker,但我不想要任何服务器端代码。 –
如果您尝试从客户端进行身份验证,那么为什么使用由于CORS而不是隐式授权而需要服务器端应用程序的客户端凭证?也许我误解了这个问题。 –