[英]How to get one access token/refresh token for indefinite use (oauth2 - Spotify API)
[英]How can I get an access token Spotify API?
我一直在看Spotify api几天和示例源代码,我仍然无法弄清楚如何获取访问令牌来访问用户的播放列表数据。 我已经达到了拉起登录窗口,用户登录,然后我收到授权代码的程度。 此时,我尝试过这样的事情:
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很糟糕。 请帮忙! 谢谢!
(编辑)我忘了提到:
链接到api身份验证指南: https : //developer.spotify.com/web-api/authorization-guide/
我坚持第4步。我看到有一种替代方法可以发送可能有效的“头参数”或cURL请求。 但是因为我不知道如何做这些事情我一直坚持发送client_id和client_secret作为身体请求参数,就像我之前为用户登录/代码所做的那样。
PS:我只是在使用这个应用程序,我正在为自己写作。 有没有办法在不经过这个过程的情况下对令牌进行硬编码?
收到授权代码后,您需要通过向Spotify帐户服务发出POST请求 ,将其与访问令牌交换,这次是对其/ api / token端点:
因此,您需要使用请求正文中的参数向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...
},
}
);
(作为旁注, “意外状态:405”是指HTTP状态代码405 Method Not Allowed ,表示您尝试的请求方法 - GET请求 - 不允许在该URL上。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.