2017-08-01 107 views
0

我試圖使用請求模塊向https://accounts.spotify.com/api/token發出POST請求以獲取訪問令牌。我已經使用我的Spotify開發者帳戶註冊了重定向URI。這是我的快遞/redirect路線。無法從Spotify API獲取訪問令牌

const Request = require('request'); 

module.exports = (req, res) => { 
    const data = { 
    grant_type: 'authorization_code', 
    code: req.query.code, 
    redirect_uri: 'http://localhost:3000/redirect', 
    client_id: process.env.SPOTIFY_ID, 
    client_secret: process.env.SPOTIFY_SECRET 
    } 

    const options = { 
    method: 'POST', 
    url: 'https://accounts.spotify.com/api/token', 
    json: true, 
    body: data 
    } 

    Request(options, (error, response, body) => { 
    if (error) return console.log(error); 
    res.end(body); 
    }); 
}; 

任何人都可以看到什麼可能是錯誤的嗎?我得到的只是一個不起眼的'哎呀!每次都出錯了'錯誤頁面。

回答

0

的數據參數有一個表格數據將被傳遞:

const options = { 
    method: 'POST', 
    url: 'https://accounts.spotify.com/api/token', 
    json: true, 
    form: data 
} 
+0

遺憾的是沒有 - 這些文檔還提到,您可以添加CLIENT_ID和client_secret在POST體。沒有任何區別把他們作爲標題:/ –

+0

@ J.Davies你是對的,跳過那部分。我發現這個問題,會編輯我的帖子 – Teh

+0

啊輝煌,謝謝! –