我试图让我的应用通过他们的Authorization Code Flow登录Spotify。我可以在初始授权步骤中从Spotify接收授权<div class="col" repeat.for="[item] of range(0, 25)">
<label>
<input type="checkbox" model.bind="item" checked.bind="selectedNumbers" class="d-none">
<span class="boxList ${selectedNumbers.indexOf(item) ? 'active': ''}">${item | numberFormat}</span>
</label>
</div>
,但在尝试获取访问令牌时收到以下错误:
code
我的代码如下:
{"error":"invalid_client","error_description":"Invalid client"}
然后,我正在发布以下内容:
# Callback from Spotify Authorization
get '/auth/spotify/callback' do
session[:code] = params[:code]
redirect to '/refresh'
end
任何帮助将不胜感激
编辑:我也尝试使用PostMan在上面发布相同的参数,但收到相同的错误消息
答案 0 :(得分:1)
您需要在POST请求中添加Authorization标头。
在Net :: HTTP.post选项中添加以下密钥:
{'Authorization' => 'Basic YOUR_AUTH_CODE' }
编辑:
这是'Your application requests refresh and access tokens'标题下的文档。
答案 1 :(得分:1)
回答我自己的问题:
我不需要提出请求,因为我使用的gem 'omniauth-spotify'
可以在request.env['omniauth.auth'].credentials.token
我也错误地创建了POST请求。下面的示例是进行POST并从refresh_token
获取新令牌的正确方法(在上面的.credentials
哈希中提供)
# Get new access token from refresh token
# session[:creds] = request.env['omniauth.auth'].credentials
get '/refresh' do
refresh_token = session[:creds].refresh_token
auth = "Basic " + Base64.strict_encode64("#{client_id}:#{client_secret}")
uri = URI.parse('https://accounts.spotify.com/api/token')
request = Net::HTTP::Post.new(uri)
request["Authorization"] = auth
request.set_form_data(
"grant_type" => "refresh_token",
"refresh_token" => refresh_token,
)
req_options = {
use_ssl: uri.scheme == "https",
}
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
"#{response.code}" # > 200 OK
end