使用23andMe API

时间:2017-12-12 06:41:35

标签: ruby authentication 23andme-api

23andme网站有一个API,他们提供following instructions

  

使用这些参数发送POST /令牌/请求(client_id和   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"}

我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

通常在获取令牌时使用OAuth2,给定的重定向URI应与OAuth应用程序的重定向URI匹配。该错误表示它不匹配。

您还要为请求设置'Content-Type' => 'application/json'标题,您的意思是'Accept' => 'application/json'吗?