如何获取访问令牌Spotify API?

时间:2022-04-13 20:26:21

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:我只是在使用这个我自己写的应用程序。有没有办法在不经过这个过程的情况下对令牌进行硬编码?

1 个解决方案

#1


4  

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帐户服务发出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上。)

#1


4  

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帐户服务发出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上。)