I've been tasked with rebuilding this javascript file into ruby but I'm getting a response that doesn't make sense (to me) the error is:
{"api_error_code"=>1, "message"=>"Parameter 'username_or_email' is required.", "code"=>"API_ERROR", "target"=>"<webapp.resources.user_session_resource.UserSessionResource object at 0xb8e2a90>", "http_status"=>400}
It doesn't make sense becuase I am indeed supplying the username_or_email
parameter as you can see here where I do a puts on the body before making the request:
{"source_url":"/login/","data":"{\"options\":{\"username_or_email\":\"[email protected]\",\"password\":\"testpassword\"},\"context\":{}}","module_path":"App()>LoginPage()>Login()>Button(class_name=primary, text=Log In, type=submit, size=large)"}
It also doesn't seem to matter how I set the payload, either by doing @curl.post_body = payload
or @curl.http_post(payload)
or @curl.http_post(url,payload)
it just responds with the same error. Incidentally @curl
is an instance of Curl::Easy
.
I've matched the json data request as the original has it so what's going on?
Here's the request so far:
require_relative "curb_dsl"
require 'json'
module Pin
class Client
Login_URL = "https://www.pinterest.com/resource/UserSessionResource/create/"
Repin_URL = "https://www.pinterest.com/resource/RepinResource/create/"
include Curb_DSL
class << self
def login(username_or_email, password)
self.new do
set_uri Login_URL
header 'Accept', 'application/json, text/javascript, */*; q=0.01'
header 'Accept-Language', 'en-US,en;q=0.5'
header 'Cache-Control', 'no-cache'
header 'DNT','1'
header 'Host', 'www.pinterest.com'
header 'Origin', 'https://www.pinterest.com'
header 'Referer', 'https://www.pinterest.com/'
header 'User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'
header 'X-APP-VERSION', '18733c1'
header 'X-CSRFToken', 'K4C0QUu35Eoq1xjajbMluw7hOKibpQSW'
header 'X-NEW-APP', '1'
header 'X-Pinterest-AppState', 'active'
header 'X-Requested-With', 'XMLHttpRequest'
set_cookies({ ":_auth" => '0',csrftoken: 'K4C0QUu35Eoq1xjajbMluw7hOKibpQSW'})
set_payload({
source_url: "/login/",
data: {
options: {
username_or_email: username_or_email,
password: password
},
context: {}
}.to_json,
module_path: "App()>LoginPage()>Login()>Button(class_name=primary, text=Log In, type=submit, size=large)"
})
set_type_converter -> (payload) {payload.to_json}
set_error_handler -> {
JSON.parse(body)['resource_response']['error'].to_s
}
puts post_body
post
@csrftoken = resp_cookies['csrftoken']
end
end
end
end
end
Ok, So I found the problem, after analyzing the same thing as the javascript but written in PHP (link) I realized that I was posting the data incorrectly, I needed to send the data, not as JSON but as query string (k=v&k=v) changing:
set_type_converter -> (payload) {payload.to_json}
to
set_type_converter -> (payload) {query_params(payload)}
which results in the post_body
being:
source_ury=%2Flogin%2F&data=%7B%22options%22%3A%7B%22username_or_email%22%3A%22testusername%40host.com%22%2C%22password%22%3A%22testpassword%22%7D%2C%22context%22%3A%7B%7D%7D&module_path=App%28%29%3ELoginPage%28%29%3ELogin%28%29%3EButton%28class_name%3Dprimary%2C+text%3DLog+In%2C+type%3Dsubmit%2C+size%3Dlarge%29
It seems to have solved the problem.