Techioz Blog

Spotify API が {“error”:“unsupported_grant_type”,“error_description”:“grant_type パラメータが見つかりません”} を返すリフレッシュ トークンのポスト リクエスト (Ruby)

概要

リフレッシュトークンを求めてSpotify APIを呼び出していますが、一貫してメソッド{“error”:“unsupported_grant_type”,“error_description”:“grant_typeparameter is missing”}を取得します。 私のコードはgrant_type: ’refresh_token’と読み取っていますが、ドキュメントを読んだ後でも問題についてよくわかりません。助けていただければ幸いです。

これはこれまでの私のコードです

@response = HTTParty.post("https://accounts.spotify.com/api/token",
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      body: {
        grant_type: 'refresh_token',
        refresh_token: ENV['REFRESH_TOKEN'],
        client_id: ENV['CLIENT_ID']
      }.to_json)

解決策

トークンを更新するリクエストは、承認を得て行う必要があります。

@response = HTTParty.post("https://accounts.spotify.com/api/token",
  headers: {
    'Content-Type' => 'application/x-www-form-urlencoded',
    'Authorization' => "Basic #{Base64.strict_encode64("#{ENV['CLIENT_ID']}:#{ENV['CLIENT_SECRET']}")}"
  },
  body: {
    grant_type: 'refresh_token',
    refresh_token: ENV['REFRESH_TOKEN'],
  }
)