23andMe网站具有API,它们提供以下说明:
用这些参数发送帖子/令牌/请求(client_id and client_secret在您的仪表板上):
curl https://api.23andme.com/token/
-d client_id=xxx
-d client_secret=yyy
-d grant_type=authorization_code
-d code=zzz
-d "redirect_uri=https://localhost:5000/receive_code/"
-d "scope=basic%20rs3094315"
这是我的代码:
require 'net/http'
require 'openssl'
require 'json'
def get_token
uri = URI.parse("https://api.23andme.com/token")
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(uri.path,
initheader = {'Content-Type' =>'application/json'})
request.set_form_data({"client_id" => 'bc81e2eb4fa77a0a8a2344aa99b5fb1e',
"client_secret" => 'd19160ac880a0d7a13abb87b90f66d8e',
"grant_type" => 'authorization_code',
"code" => 'a210a1d0beba7e3a1ef7f4133d7a3d3c',
"redirect_uri" => 'http://localhost:3000',
"scope" => 'names basic haplogroups relatives'})
response = https.request(request)
data = JSON.load response.read_body
return data
end
puts get_token
非常简单,但是我一直遇到以下错误:
{"error_description"=>"redirect_uri doesn't match", "error"=>"invalid_request"}
我在这里缺少什么?
通常在获取令牌时使用oauth2,给定重定向的URI应与OAuth应用程序的重定向URI匹配。错误表明它不匹配。
您还为请求设置'Content-Type' => 'application/json'
标头,您是指'Accept' => 'application/json'
吗?