2017-08-01 113 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

残念ながらありません - それらのドキュメントはまた、あなたがPOST本体でのclient_idとclient_secretを追加できることに言及します。彼らをヘッダとして入れても差はありませんでした。/ –

+0

@ J.Daviesあなたはそうです、その部分をスキップしました。私は問題を見つけ、私の投稿を編集します – Teh

+0

ああ素晴らしい、ありがとう! –

関連する問題