我尝试通过几个宝石向用户发送消息,但我得到了Faraday :: Error。
有没有办法在没有宝石的情况下发送它?怎么样?
我使用ruby 2.0.0和rails 4.1.6。
答案 0 :(得分:1)
您可以使用Net::HTTP发送。
确保您拥有范围w_messages
# token : user's token (who is going to send the message)
# subject : Subject of the message
# body : body of the message
# recipient_ids : array of recipient ids (on linkedin)
require "net/http"
def send_linkedin_message(token, subject, body, recipient_ids)
path = "https://api.linkedin.com/v1/people/~/mailbox"
message = {
subject: subject,
body: body,
recipients: {
values: recipient_ids.map do |profile_path|
{person: {_path: "/people/#{profile_path}"} }
end
}
}
uri = URI.parse(path)
request = Net::HTTP::Post.new(uri)
request.body = message.to_json
request.initialize_http_header({ "x-li-format" => "json", "Authorization" => "Bearer #{token}" })
request.content_type = 'application/json'
response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') {|http| http.request request}
end