[英]Spotify oauth is returning a 400 when trying to get access_token
I am trying to get an access_token from the spotify api.我正在尝试从 spotify api 获取 access_token。 spotify docs
发现文档
However i am always getting a response 400, {"error"=>"invalid_grant", "error_description"=>"Invalid authorization code"}
但是我总是收到响应 400,
{"error"=>"invalid_grant", "error_description"=>"Invalid authorization code"}
The code i am running was originally, the authorization_code is the from the controller and it is what is returned in the params[:code]
我正在运行的代码最初是,authorization_code 来自 controller,它是
params[:code]
中返回的内容
response = HTTParty.post(
'https://accounts.spotify.com/api/token',
body: {
grant_type: 'authorization_code',
code: authorization_code,
redirect_uri: redirect_uri
}
),
headers: {
'Authorization' => 'Basic ' + Base64.strict_encode64("#{client_id}:#{client_secret}"),
}
)
I read about form encoding so i tried我阅读了有关表单编码的信息,所以我尝试了
response = HTTParty.post(
'https://accounts.spotify.com/api/token',
body: URI.encode_www_form(
{
grant_type: 'authorization_code',
code: authorization_code,
redirect_uri: redirect_uri
}
),
headers: {
'Authorization' => 'Basic ' + Base64.strict_encode64("#{client_id}:#{client_secret}"),
'Content-Type' => 'application/x-www-form-urlencoded'
}
)
but this has been to no avail... i have tried what i can think of as every combination and cannot get it to work.但这无济于事......我已经尝试了我能想到的每一种组合,但无法让它发挥作用。
The redirect_uri is definitely right as when i change that it gives a bad_uri method and the authorization works as i can query the api with Implicit Grant
redirect_uri 绝对正确,因为当我更改它提供 bad_uri 方法并且授权有效时,我可以使用
Implicit Grant
查询 api
If anyone has any experience/solutions i would be much obliged如果有人有任何经验/解决方案,我将不胜感激
I think you don't need to encode the body on your own.我认为你不需要自己编码身体。 Setting the header should be enough.
设置 header 应该足够了。 You may have to URL encode the
redirect_uri
, as it must match with the value used in the first/authorization code request.您可能必须对
redirect_uri
进行 URL 编码,因为它必须与第一个/授权代码请求中使用的值匹配。
response = HTTParty.post('https://accounts.spotify.com/api/token',
body: {
grant_type: 'authorization_code',
code: authorization_code,
redirect_uri: redirect_uri
},
headers: {
'Authorization' => 'Basic ' + Base64.strict_encode64("#{client_id}:#{client_secret}"),
'Content-Type' => 'application/x-www-form-urlencoded'
}
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.