2016-06-07 106 views
0

我正在使用react-native(並通過使用內置的fetch方法進行本質化操作。我有以下代碼可以發佈到spotify,並且應該返回一個令牌令牌的我收到一個錯誤react-native從spotify API獲取訪問/刷新令牌

代碼:

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…} 
+0

您確定您發佈的代碼爲「resCode」是正確的嗎? –

回答

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

+0

你知道我該如何編輯我使用fetch發送參數與json字符串? – httpNick

+0

我認爲你可以做'var data = new FormData(); data.append('code',resCode);'並且對剩下的參數進行相同操作。根據文檔,您可以簡單地在請求中傳遞FormData作爲body:'body:data'。 – xjmdoo

+0

嘗試後,我得到相同的錯誤。 – httpNick