0

23andme 网站有一个 API,他们提供以下说明

使用以下参数发送 POST /token/ 请求(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"}

我在这里想念什么?

4

1 回答 1

0

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

您还在为请求设置'Content-Type' => 'application/json'标头,您的意思是'Accept' => 'application/json'

于 2017-12-12T12:37:17.030 回答