简体   繁体   English

Spotify Web API:基本授权

[英]Spotify Web API: Basic authorization

I need to get an access token from Spotify Web API . 我需要从Spotify Web API获取访问令牌。 Based on this documentation I wrote the following method: 基于此文档,我编写了以下方法:

def authorize
  grant = Base64.encode64("#{SPOTIFY_KEY}:#{SPOTIFY_SECRET}")
  RestClient::Request.execute(
    method: :post,
    url: 'https://accounts.spotify.com/api/token',
    params: {'grant_type' => 'client_credentials'},
    headers: {"Authorization" => "Basic #{grant}","Accept" => "*/*; q=0.5, application/json"}
  )
end

and the following RSpec test: 以及以下RSpec测试:

it 'authorize' do
  obj = SpotifyIntegration.new
  response = obj.authorize
  myjson = JSON.parse(response.body)
  expect(myjson.has_key?('access_token')).to be(true)
  expect(myjson.has_key?('token_type')).to be(true)
  expect(myjson['token_type']).to eq('bearer')
  expect(myjson.has_key?('expires_in')).to be(true)
end

It happens that when I run this test, generating this request (caught with RESTCLIENT_LOG=stdout) 碰巧,当我运行此测试时,生成此请求(被RESTCLIENT_LOG = stdout捕获)

RestClient.post " https://accounts.spotify.com/api/token ", "Accept"=>" / ; q=0.5, application/json", "Accept-Encoding"=>"gzip, deflate", "Authorization"=>"Basic Y2NmNTI3ODVlZWI1NDVlODk0ZmM2ZTY3YTZhNDM0ZDA6YTQ5MjdlOGFmOWQy\\nNGE0OTgyZDRkODI1MmJhZjBkNTI=\\n" RestClient.post“ https://accounts.spotify.com/api/token ”,“ Accept” =>“ / ; q = 0.5,application / json”,“ Accept-Encoding” =>“ gzip,deflate”,“授权“ =>”基本Y2NmNTI3ODVlZWI1NDVlODk0ZmM2ZTY3YTZhNDM0ZDA6YTQ5MjdlOGFmOWQy \\ nNGE0OTgyZDRkODI1MmJhZjBkNTI = \\ n“

I get 我懂了

=> 400 BadRequest | => 400 BadRequest | application/json 131 bytes application / json 131字节

And it seems it is really a bad request, because I see no sign of the grant_type => client_credentials . 看来这确实是一个错误的请求,因为我看不到grant_type => client_credentials迹象。 The documentaions says this is mandadory as a request body parameter . 文件说这是要求作为参数的要求

I believe I am sending this the wrong way, but I don't know how to to it correctly. 我相信我发送的方式有误,但我不知道如何正确发送。

I tried to use RestClient#post instead of RestClient::Request#execute , doing this: 我试图使用RestClient#post代替RestClient::Request#execute ,这样做:

def authorize
  grant = Base64.encode64("#{SPOTIFY_KEY}:#{SPOTIFY_SECRET}")
  RestClient.post 'https://accounts.spotify.com/api/token', {'grant_type' => 'client_credentials'}.to_json, {"Authentication" => "Basic #{grant}",content_type: :json, accept: :json}
end

but then I got: 但是我得到了:

RestClient::UnsupportedMediaType: 415 Unsupported Media Type RestClient :: UnsupportedMediaType:415不支持的媒体类型

How may I send a request body parameter using RestClient gem? 如何使用RestClient gem发送请求正文参数?

The issue is the way Base64 is encoding the string which includes newlines that most OAuth2 providers don't accept. 问题是Base64编码字符串的方式,其中包括大多数OAuth2提供程序不接受的换行符。 You can do this instead: 您可以改为:

grant = Base64.encode64("#{client_id}:#{client_secret}").delete("\n")

resp = RestClient.post('https://accounts.spotify.com/api/token',
                       {'grant_type' => 'client_credentials'},
                       {"Authorization" => "Basic #{grant}"})

According to this answer, new lines are added every 60 characters (that is news to me). 根据答案,每60个字符添加一行新行(这对我来说是个新闻)。 You can use another method that does not include newlines such as strict_encode ... 您可以使用不包含换行符的其他方法,例如strict_encode ...

grant = Base64.strict_encode64("#{client_id}:#{client_secret}")

It's not exactly the answer you're looking for, but you should definitively take a look at this Ruby Library: rspotify . 这并不是您要找的答案,但是您应该绝对看看以下Ruby库: rspotify Spotify Oauthentication is very easy to do with it. Spotify Oauthentication非常易于使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM