【问题标题】:Spotify Web API Bad Request Error "invalid_client" when refreshing token刷新令牌时 Spotify Web API 错误请求错误“invalid_client”
【发布时间】:2014-10-25 03:46:21
【问题描述】:

我正在使用 Spotify Web API 在 Rails 中构建一个应用程序。我构建了一种刷新用户令牌的方法,但收到 400 错误。根据 Spotify Web API 文档,我的请求标头需要采用以下格式:

Authorization: Basic <base64 encoded client_id:client_secret>

使用 Httparty gem,这里是刷新访问令牌的 POST 方法:

def refresh_token
client_id = "foo"
client_secret = "bar"
client_id_and_secret = Base64.encode64("#{client_id}:#{client_secret}")
result = HTTParty.post(
    "https://accounts.spotify.com/api/token",
    :body => {:grant_type => "refresh_token",
              :refresh_token => "#{self.oauth_refresh_token}"},
    :headers => {"Authorization" => "Basic #{client_id_and_secret}"}
    )
end

“结果”最终是这样的:

=> #<HTTParty::Response:0x7f92190b2978 parsed_response={"error"=>"invalid_client", "error_description"=>"Invalid client secret"}, @response=#<Net::HTTPBadRequest 400 Bad Request readbody=true>, @headers={"server"=>["nginx"], "date"=>["Sun, 31 Aug 2014 22:28:38 GMT"], "content-type"=>["application/json"], "content-length"=>["70"], "connection"=>["close"]}>

我可以解码 client_id_and_secret 并返回“foo:bar”,所以我不知道为什么会收到 400 错误。非常感谢任何见解。

【问题讨论】:

    标签: ruby-on-rails spotify httparty


    【解决方案1】:

    发现问题...它与 Ruby 中的 Base64 编码有关。显然(如Strange \n in base64 encoded string in Ruby 所示)使用 Base64.encode64('') 方法在代码中添加了额外的一行。使用 Base64.strict_encode64('') 解决了这个问题。

    更新代码:

    def refresh_token
    client_id = "foo"
    client_secret = "bar"
    client_id_and_secret = Base64.strict_encode64("#{client_id}:#{client_secret}")
    result = HTTParty.post(
        "https://accounts.spotify.com/api/token",
        :body => {:grant_type => "refresh_token",
                  :refresh_token => "#{self.oauth_refresh_token}"},
        :headers => {"Authorization" => "Basic #{client_id_and_secret}"}
        )
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-15
      • 1970-01-01
      • 2018-02-18
      • 1970-01-01
      • 1970-01-01
      • 2022-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多