【问题标题】:How to POST to exchange a 23andme auth code for a token如何发布以将 23andme 身份验证代码交换为令牌
【发布时间】:2014-03-19 19:09:31
【问题描述】:

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"

如果成功,您将收到 200 JSON 响应,如下所示:

{"access_token":"89822b93d2",
 "token_type":"bearer",
 "expires_in": 86400,
 "refresh_token":"33c53cd7bb",
 "scope":"rs3094315 basic"}

所以,这就是我尝试过的方法,但没有奏效。我知道 Ruby,但从未使用过 net/http 或 uri。

def self.get_token(code)
    uri = URI.parse("https://api.23andme.com/token")
    https = Net::HTTP.new(uri.host, uri.port)
    https.use_ssl = true

    request = Net::HTTP::Post.new(uri.path)

    request["client_id"]     = 'my client id goes here'
    request["client_secret"] = 'my client secret goes here'
    request["grant_type"]    = 'authorization_code'
    request["code"]          = code
    request["redirect_uri"]  = 'http://localhost:3000'
    request["scope"]         = 'names basic haplogroups relatives'

    response = https.request(request)
    return response.to_s
end

【问题讨论】:

  • 这是另一回事 - 我认为我的回答解决了您最初的问题。尝试在https.use_ssl = true行之后设置http.verify_mode = OpenSSL::SSL::VERIFY_NONE

标签: ruby https uri 23andme-api


【解决方案1】:

您使用curl-d 选项来设置POST 请求正文中的参数是正确的。但是,对于Net::HTTP::Post 对象,语法request["key"] = value 用于设置Http 对象的headers

使用set_form_data 将所有参数设置到POST 请求的body 中。

比如这样使用:

request.set_form_data({"client_id" => "my client id goes here", "code" => code})

以下将澄清上述内容:

$ > request["client_id"] = 'my client id goes here'
# => "my client id goes here" 
$ > request.body
# => nil # body is nil!!!
$ > request.each_header { |e| p e }
# "client_id"
# => {"client_id"=>["my client id goes here"]} 

$ > request.set_form_data("client_secret" => 'my client secret goes here')
# => "application/x-www-form-urlencoded" 
$ > request.body
# => "client_secret=my+client+secret+goes+here" # body contains the added param!!!

【讨论】:

  • 感谢您的回复。我已经实现了这个,它现在返回一个#<:httpbadrequest:0x3fea2b8>。 https.verify_mode mod 有所作为。肯定只有一件小事还是错的。
【解决方案2】:

我只是想发布有效的代码。感谢拉法。

def self.get_token(code)
    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"     => 'my client id',
                           "client_secret" => 'my client secret',
                           "grant_type"    => 'authorization_code',
                           "code"          => code,
                           "redirect_uri"  => 'http://localhost:3000/receive_code/',
                           "scope"         => 'names basic haplogroups relatives'})

    response = https.request(request)
    data = JSON.load response.read_body

    return data["access_token"]
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    • 2018-10-27
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-17
    相关资源
    最近更新 更多