从Parse Rest API的响应中获取会话令牌

时间:2015-08-02 11:19:51

标签: ruby-on-rails ruby rest curl parse-platform

我跟踪https://parse.com/docs/rest/guide用于登录用户。我的问题是:如何获取当前登录用户?

到目前为止,这是我的代码:

class LoginController < ApplicationController
  def index

  end
  def log_in

    headers = {"X-Parse-Application-Id" => "API_KEY", 
                         "X-Parse-REST-API-Key" => "API_KEY"}
    query = {:username => params[:user][:username], 
                     :password => params[:user][:password]}

            @response = HTTParty.get('https://api.parse.com/1/login', 
        :query => query,
            :headers => headers)
   end
end

@response每次刷新浏览器时都返回用户信息以及会话令牌,这是新的?是对的吗?如果它是正确的,如何从@response中获取会话令牌,以便我可以将它用于以后的REST API?

1 个答案:

答案 0 :(得分:1)

@response是哈希,您可以从中获取sessionToken

@response["sessionToken"]

如果您使用@response检查p @response,您会看到以下内容:

#<HTTParty::Response:0x2d56590 parsed_response={"createdAt"=>"2015-08-01T10:11:38.439Z", "objectId"=>"ZLIk9hKmki", "phone"=>"12345-67890", "sessionToken"=>"r:RIWzQiY3kGhRPDtaLmjDizZog", "updatedAt"=>"2015-08-01T10:11:38.439Z", "username"=>"some_user_id"}, @response=#<Net::HTTPOK 200 OK readbody=true>, @headers={"access-control-allow-methods"=>["*"], "access-control-allow-origin"=>["*"], "content-type"=>["application/json; charset=utf-8"], "date"=>["Sun, 02 Aug 2015 12:58:39 GMT"], "server"=>["nginx/1.6.0"], "x-parse-platform"=>["G1"], "x-runtime"=>["0.136318"], "content-length"=>["191"], "connection"=>["Close"]}>

因此,您可以通过执行以下操作来访问其他属性

p @response["phone"]
p @response["username"]