2

axios httpライブラリを使用するために変換しようとしている小さな Spotify アプリがあります。ログイン時のコールバックに問題があります。これまで、すべてのドキュメントrequestで like is を使用してきました。Spotifyすべてが で正常に動作しrequestますが、すべてが で同じように見えますがaxios500 Internal Server Error. httpリクエストを行うための私のコードは次のとおりです。

var authOptions = {
    method: 'POST',
    url: 'https://accounts.spotify.com/api/token',
    form: {
        code: code,
        redirect_uri: REDIRECT_URI,
        grant_type: 'authorization_code'
    },
    headers: {
        'Authorization': 'Basic ' + (new Buffer(CLIENT_ID + ':' + CLIENT_SECRET).toString('base64'))
    },
    json: true
};

axios(authOptions).then(res => {
    console.log(res)
})

authOptions同じオブジェクトをrequestライブラリに渡すことができ、すべて正常に動作します。axiosコンソールにログアウトしてからの私のリクエストは次のとおりです。

{ 
  method: 'POST',
  url: 'https://accounts.spotify.com/api/token',
  form: 
   { code: 'changedthecode',
     redirect_uri: 'http://localhost:8888/callback',
     grant_type: 'authorization_code' },
  headers: { Authorization: 'Basic changedthecode=' },
  json: true,
  timeout: 0,
  transformRequest: [ [Function] ],
  transformResponse: [ [Function] ],
  withCredentials: undefined 
}

そして、これがaxiosライブラリに対する私の応答です。

{ data: { error: 'server_error' },
  status: 500,
  statusText: 'Internal Server Error',
  headers: 
   { server: 'nginx',
     date: 'Fri, 04 Dec 2015 14:48:06 GMT',
     'content-type': 'application/json',
     'content-length': '24',
     connection: 'close' },
  config: 
   { method: 'POST',
     headers: { Authorization: 'Basic changedthecode' },
     timeout: 0,
     transformRequest: [ [Function] ],
     transformResponse: [ [Function] ],
     url: 'https://accounts.spotify.com/api/token',
     form: 
      { code: 'changedthecode',
        redirect_uri: 'http://localhost:8888/callback',
        grant_type: 'authorization_code' },
     json: true,
     withCredentials: undefined } 
}

from について私が知らなかった唯一のオプションaxiosは、またはwithCredentialsに設定されている場合は機能しませんでした。他に何が欠けていますか?falsetrue

4

1 に答える 1

2

問題は、フォームを投稿していて、ネットワークを通過するときにそれをエンコードしておらず、Content-Type.. 私は次のように変更authOptionsしました:

var authOptions = {
    method: 'POST',
    url: 'https://accounts.spotify.com/api/token',
    data: querystring.stringify({
            grant_type: 'refresh_token',
            refresh_token: refreshToken
        }),
    headers: {
        'Authorization': 'Basic ' + (new Buffer(CLIENT_ID + ':' + CLIENT_SECRET).toString('base64')),
        'Content-Type': 'application/x-www-form-urlencoded'
    },
    json: true
};

そしてすべてがうまくいきました。

于 2015-12-05T22:32:17.410 に答える