在client_credentials Spotify API请求上获取400个错误状态代码

时间:2019-12-04 10:33:36

标签: javascript jquery api spotify

我正在使用Spotify的API,但未成功自动获取新的访问令牌。

var arr = [
  {
    id: '10cd',
    name: 'item 1'
    children: [
      {id: '11v', name1: 'Item 1 child 1'},
      {id: '12c', name1: 'Item 1 child 2'}
    ]
  },
  {
    id: '13v',
    name: 'item 2'
    children: [
      {id: '26e', name1: 'Item 2 child 1'},
      {id: '7a', name1: 'Item 2 child 2'}
    ]
  }
]
function api() {
  $.ajax({
    type: 'POST',
    url: 'https://accounts.spotify.com/api/token?grant_type=client_credentials',
    headers: {
    'Authorization': 'Basic myClientIdEncodedToBase64Format:myClientSecretEncodedToBase64Format'
     },
    success: function(data) {
     console.log('Succes ->', data);
    },
    error: function(error) {
      console.log('Error -> ', error);
    }
  });
}

我遇到400错误。如果[i引用文档] [1],则为'400:错误的请求-由于语法格式错误,服务器无法理解该请求。邮件正文将包含更多信息。

我在这里做什么错了?

谢谢!

1 个答案:

答案 0 :(得分:0)

这是我的代码。我添加数据:

function api() {
          $.ajax({
            type: 'POST',
            url: 'https://accounts.spotify.com/api/token', 
            data: 'grant_type=client_credentials',
            headers: {
              'Authorization': 'Basic myClientIdAndMyClientSecretConvertedToBase64'
            },
            success: function(data) {
              console.log('Succes ->', data);
            },
            error: function(error) {
              console.log('Error -> ', error);
            }
          });
        }