Javascript 如何获得访问令牌 Spotify API?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39887342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get an access token Spotify API?
提问by Ashwin Gupta
I've been looking at the Spotify api for a few days now and example source code and I still can't figure out how to get an access token to access a user's playlist data. I've gotten to the point where I pull up the login window, the user logs in, and then I receive an authorization code. At this point, I tried doing things like:
我几天来一直在查看 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");
and
和
$.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");
}
}
);
But either way I get a result like:
但无论哪种方式,我都会得到如下结果:
{"error":"server_error","error_description":"Unexpected status: 405"}
instead of a token. I'm sure this is simple but I'm terrible at JS. Please help! Thank you!
而不是令牌。我确定这很简单,但我对 JS 很糟糕。请帮忙!谢谢!
(edit) I forgot to mention:
(编辑)我忘了提到:
Link to api authentication guide: https://developer.spotify.com/web-api/authorization-guide/
api认证指南链接:https: //developer.spotify.com/web-api/authorization-guide/
I'm stuck on step 4. I see that there is an alternative method on sending a "header parameter" or cURL request which might work. But seing as how I have no idea how to do these things I've stuck with sending the client_id and client_secret as body request parameters like I did before for the user login/code.
我被困在第 4 步。我看到有一种发送“标头参数”或 cURL 请求可能有效的替代方法。但是,由于我不知道如何做这些事情,我一直坚持将 client_id 和 client_secret 作为正文请求参数发送,就像我之前为用户登录/代码所做的那样。
PS: I'm only using this application I'm writing for myself. Is there a way I could hardcode a token in without going through this process instead?
PS:我只使用我为自己编写的这个应用程序。有没有办法可以在不经过此过程的情况下对令牌进行硬编码?
采纳答案by Frxstrem
When the authorization code has been received, you will need to exchange it with an access token by making a POST requestto the Spotify Accounts service, this time to its /api/token endpoint:
收到授权代码后,您需要通过向 Spotify Accounts 服务发出 POST 请求(这次是向其 /api/token 端点)与访问令牌交换它:
So you need to make a POST request to the Spotify API, with the parameters in the request body:
因此,您需要向 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...
},
}
);
(As a sidenote, "Unexpected status: 405"refers to the HTTP status code 405 Method Not Allowed, which indicates that the request method you tried—a GET request—is not allowed on that URL.)
(作为旁注,“意外状态:405”指的是 HTTP 状态代码405 Method Not Allowed,这表明您尝试的请求方法 - GET 请求 - 在该 URL 上是不允许的。)