API 未按预期响应

API not responding as expected

我的任务是将 this javascript file 重建为 ruby,但我收到的回复(对我来说)没有意义,错误是:

{"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}

这没有意义,因为我确实提供了 username_or_email 参数,正如您在此处看到的那样,我在发出请求之前对 body 进行了放置:

{"source_url":"/login/","data":"{\"options\":{\"username_or_email\":\"testusername@host.com\",\"password\":\"testpassword\"},\"context\":{}}","module_path":"App()>LoginPage()>Login()>Button(class_name=primary, text=Log In, type=submit, size=large)"}

我如何设置负载似乎也无关紧要,无论是通过 @curl.post_body = payload@curl.http_post(payload)@curl.http_post(url,payload) 它只是响应相同的错误。顺便说一下 @curlCurl::Easy.

的一个实例

我已经匹配了 json 数据请求,因为原来有它,这是怎么回事?

到目前为止,这是请求:

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

好的,所以我发现了问题,在分析了与 javascript 相同但写在 PHP (link) 中的内容后,我意识到我发布的数据不正确,我需要发送数据,不是 JSON 而是查询字符串 (k=v&k=v) changing:

set_type_converter -> (payload) {payload.to_json}

set_type_converter -> (payload) {query_params(payload)}

这导致 post_body 为:

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

看来问题解决了