我怎样才能获得访问令牌 Spotify API?
How can I get an access token Spotify API?
几天来我一直在研究 Spotify api 和示例源代码,但我仍然无法弄清楚如何获取访问令牌来访问用户的播放列表数据。我已经到了我拉起登录 window,用户登录,然后我收到授权码的地步。在这一点上,我尝试做这样的事情:
window.open("https://accounts.spotify.com/api/token?
grant_type=authorization_code&code="+code+"&redirect_uri=myurl&client_id=3137b15
2f1424defa2c6020ae5c6d444&client_secret=mysecret");
和
$.ajax(
{
url: "https://accounts.spotify.com/api/token?grant_type=authorization_code&code="+code+"&redirect_uri=myurl&client_secret=mysecret&client_id=myid",
success: function(result){
alert("foo");
}
}
);
但无论哪种方式,我都会得到这样的结果:
{"error":"server_error","error_description":"Unexpected status: 405"}
而不是令牌。我敢肯定这很简单,但我在 JS 方面很糟糕。请帮忙!谢谢!
(编辑)我忘了说:
Link 到 api 身份验证指南:https://developer.spotify.com/web-api/authorization-guide/
我卡在了第 4 步。我发现有一种替代方法可以发送 "header parameter" 或 cURL 请求,该方法可能有效。但是由于我不知道如何做这些事情,所以我坚持发送 client_id 和 client_secret 作为正文请求参数,就像我之前为用户 login/code 所做的那样。
PS:我只使用我为自己编写的这个应用程序。有没有一种方法可以在不经过此过程的情况下对令牌进行硬编码?
When the authorization code has been received, you will need to exchange it with an access token by making a POST request to the Spotify Accounts service, this time to its /api/token endpoint:
因此您需要向 Spotify API 发出 POST 请求,请求正文中的参数为:
$.ajax(
{
method: "POST",
url: "https://accounts.spotify.com/api/token",
data: {
"grant_type": "authorization_code",
"code": code,
"redirect_uri": myurl,
"client_secret": mysecret,
"client_id": myid,
},
success: function(result) {
// handle result...
},
}
);
(作为旁注,"Unexpected status: 405" 指的是 HTTP 状态代码 405 Method Not Allowed,这表明您尝试过的请求方法——GET 请求——在 URL 上是不允许的。)
几天来我一直在研究 Spotify api 和示例源代码,但我仍然无法弄清楚如何获取访问令牌来访问用户的播放列表数据。我已经到了我拉起登录 window,用户登录,然后我收到授权码的地步。在这一点上,我尝试做这样的事情:
window.open("https://accounts.spotify.com/api/token?
grant_type=authorization_code&code="+code+"&redirect_uri=myurl&client_id=3137b15
2f1424defa2c6020ae5c6d444&client_secret=mysecret");
和
$.ajax(
{
url: "https://accounts.spotify.com/api/token?grant_type=authorization_code&code="+code+"&redirect_uri=myurl&client_secret=mysecret&client_id=myid",
success: function(result){
alert("foo");
}
}
);
但无论哪种方式,我都会得到这样的结果:
{"error":"server_error","error_description":"Unexpected status: 405"}
而不是令牌。我敢肯定这很简单,但我在 JS 方面很糟糕。请帮忙!谢谢!
(编辑)我忘了说:
Link 到 api 身份验证指南:https://developer.spotify.com/web-api/authorization-guide/
我卡在了第 4 步。我发现有一种替代方法可以发送 "header parameter" 或 cURL 请求,该方法可能有效。但是由于我不知道如何做这些事情,所以我坚持发送 client_id 和 client_secret 作为正文请求参数,就像我之前为用户 login/code 所做的那样。
PS:我只使用我为自己编写的这个应用程序。有没有一种方法可以在不经过此过程的情况下对令牌进行硬编码?
When the authorization code has been received, you will need to exchange it with an access token by making a POST request to the Spotify Accounts service, this time to its /api/token endpoint:
因此您需要向 Spotify API 发出 POST 请求,请求正文中的参数为:
$.ajax(
{
method: "POST",
url: "https://accounts.spotify.com/api/token",
data: {
"grant_type": "authorization_code",
"code": code,
"redirect_uri": myurl,
"client_secret": mysecret,
"client_id": myid,
},
success: function(result) {
// handle result...
},
}
);
(作为旁注,"Unexpected status: 405" 指的是 HTTP 状态代码 405 Method Not Allowed,这表明您尝试过的请求方法——GET 请求——在 URL 上是不允许的。)