0
我对HTTP GET/POST请求有点新手。我想要使用需要某种授权的获取请求。我想使用下面的API API DOCUMENTATION。带会话密钥的HTTP GET请求
在“获取列表”它说,它希望以下参数:
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请求,在标题/参数方面需要什么不同?
谢谢