2

23andme の Web サイトには 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
4

2 に答える 2

0

POSTリクエストの本文にパラメータを設定するオプションを使用curlして、正しく実行しています。-dただし、Net::HTTP::Postオブジェクトでは、構文request["key"] = valueを使用して Http オブジェクトのヘッダーを設定します。

set_form_dataすべてのパラメーターをPOST 要求の本文に設定するために使用します。

たとえば、次のように使用します。

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!!!
于 2014-03-19T19:39:42.257 に答える
0

機能するコードを投稿したかっただけです。ラファに感謝。

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
于 2014-03-21T03:50:06.820 に答える