带有会话密钥的HTTP GET请求

时间:2016-04-03 13:02:54

标签: ruby json api authentication httprequest

我有点关于HTTP GET / POST请求的新手。我想使用需要某种授权的get请求。

我正在尝试使用以下api API DOCUMENTATION

在“获取列表”下,它表示需要以下参数:

Parameters
- Accept-Language: Language prefered in the response. Note: nb and nn will return the same as no    header  string
- Authorization: Basic auth. The session_id should be sent as both username and password    header  string

我使用以下代码来授权自己,但最后一次“GET请求”会出错:

    require 'openssl'
    require 'base64'
    require 'uri'
    require 'net/http'
    require 'json'

    username = 'MYUSERNAME'
    password = 'MYPASSWORD'
    service  = 'NEXTAPI'

    # Create auth                                                                                           
    string = Base64.encode64(username) + ':' + Base64.encode64(password) + ':' + Base64.encode64((Time.now.\
    to_i * 1000).to_s)
    public_key_data = File.read(service + '_TEST_public.pem')
    public_key = OpenSSL::PKey::RSA.new(public_key_data)
    auth = URI::escape(Base64.encode64(public_key.public_encrypt(string)),
                       Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))

    # Setup HTTPS                                                                                           
    http = Net::HTTP.new('api.test.nordnet.se', 443)
    http.use_ssl = true

    # Get status of server                                                                                  
    response = http.get('/next/2/', {'Accept' => 'application/json'})
    puts response.body

    # POST login                                                                                            
    response = http.post('/next/2/login', "auth=#{auth}&service=#{service}", {'Accept' => 'application/json'})
    puts response.body
    data = JSON.parse(response.body)
    session_key = data['session_key']

    auth_string = "Basic " + session_key + ":" + session_key
    response = http.get('/next/2/lists', {'Authorization' => auth_string })
    puts response

不确定这里出了什么问题。或者我需要做什么。我收到以下错误。

#<Net::HTTPNotAcceptable:0x007fac74276d20>

问题1:如何正确发送会话密钥作为用户名和密码? 问题2:我如何实际发送参数和标题,有什么区别? 问题3:根据我是否发送GET或POST请求,标题/参数需要什么不同?

由于

1 个答案:

答案 0 :(得分:0)

Was able to solve it. Below is the OK code...

            require 'openssl'
            require 'base64'
            require 'uri'
            require 'net/https'
            require 'json'
            require 'cgi'

            username = 'USERNAME'
            password = 'PASSWORD'
            service  = 'NEXTAPI'

            def http_get(path,params)
                http = Net::HTTP.new('api.test.nordnet.se', 443)
                http.use_ssl = true

                #return http.get("#{path}?", params)
                return http.get("#{path}?")
                #return Net::HTTP.get(path)
            end

            # Create auth                                                                                           
            string = Base64.encode64(username) + ':' + Base64.encode64(password) + ':' + Base64.encode64((Time.now.\
            to_i * 1000).to_s)
            public_key_data = File.read(service + '_TEST_public.pem')
            public_key = OpenSSL::PKey::RSA.new(public_key_data)
            auth = URI::escape(Base64.encode64(public_key.public_encrypt(string)),
                               Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))

            # Setup HTTPS                                                                                           
            http = Net::HTTP.new('api.test.nordnet.se', 443)
            http.use_ssl = true

            # Get status of server                                                                                  
            response = http.get('/next/2/', {'Accept' => 'application/json'})
            puts response.body

            # POST login                                                                                            
            response = http.post('/next/2/login', "auth=#{auth}&service=#{service}", {'Accept' => 'application/json'})
            puts response.body

            data = JSON.parse(response.body)
            session_key = data['session_key']

            uri = URI('https://api.test.nordnet.se:443/next/2/lists')

            Net::HTTP.start(uri.host, uri.port,
              :use_ssl => uri.scheme == 'https', 
              :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|

              request = Net::HTTP::Get.new uri.request_uri
              request.add_field('Accept', 'application/json')
              request.basic_auth session_key, session_key

              response = http.request request # Net::HTTPResponse object

              puts response.body#  puts response.body
            end