react-native从spotify API获取访问/刷新令牌

时间:2016-06-07 07:55:43

标签: ios react-native fetch spotify

我正在使用react-native(并且本质上使用内置的fetch方法。我有以下代码发布到spotify并且应该返回一个令牌,而不是令牌我收到错误

代码:

  var btoa = require('base-64');      
  var authOptions = {
    method : 'POST',
    body: JSON.stringify({
      code: resCode,
      redirect_uri: 'myapp://foo',
      grant_type: 'authorization_code'
    }),
    headers: {
      'Authorization': 'Basic ' + btoa.encode(secrets.id + ':' + secrets.secret)
    },
    json: true
  };
  fetch('https://accounts.spotify.com/api/token', authOptions).then(
    (res) => console.log(res)
  );

错误我收到了:

Response {_bodyInit: "{"error":"unsupported_grant_type","error_descripti…redentials, authorization_code or refresh_token"}", _bodyText: "{"error":"unsupported_grant_type","error_descripti…redentials, authorization_code or refresh_token"}", type: "default", url: "https://accounts.spotify.com/api/token", status: 400…}

1 个答案:

答案 0 :(得分:0)

您正在帖子正文中发送json字符串,但是根据您必须发送参数的文档。此外,如果您查看示例卷曲请求,您可以看到它是如何完成的:

curl -H "Authorization: Basic ZjM...zE=" -d grant_type=authorization_code -d code=MQCbtKe...44KN -d redirect_uri=https%3A%2F%2Fwww.foo.com%2Fauth https://accounts.spotify.com/api/token

不是json。尝试将正文作为FormData发送。