1

我是 iOS 编程新手,使用 Rubymotion 构建我的第一个应用程序。在我的 Rubymotion 应用程序中,我 POST 到网络服务器(使用 RoR 构建的应用程序)来验证想要登录的用户,并且正在使用 BubbleWrap gem 进行 RubyMotion https://github.com/rubymotion/BubbleWrap/blob/master/motion/http .rb

def login(sender)
  payload = {email: @email, password: @password}
  BubbleWrap::HTTP.post("http://example.com/sessions", {payload:   payload}) do |response|
  @authCookie = response.headers['Set-Cookie']
 end 
end

现在,一旦我收到成功的身份验证,我就会使用以下代码从 Web 应用程序接收 JSON 数据:

BubbleWrap::HTTP.get("http://example.com/events.json", {cookie: @authCookie, :headers=>{"Content-Type"=>'json'} }) do |response|
 puts response.body #testing the response
end

由于某种原因,从 POST 请求接收到的身份验证令牌没有被 GET 请求正确传递。我知道这一点,因为在 Web 应用程序中,如果身份验证失败,它会重定向到登录页面,这就是从 GET 请求接收到的响应(登录页面的 HTML 代码)。

Web App上的身份验证检查:

   def session_check
    if session[:bizid].nil?
     redirect_to login_url
     flash[:notice] = "Please login to view your account!"
     end 
   end

此外,在 Web 应用程序上,此身份验证令牌通过以下方法设置:

def create
    current_biz = Bizname.find_by_email(params[:email])
        if current_biz && current_biz.authenticate(params[:password])
            session[:bizid] = current_biz.id
            flash[:notice] = 'Login Successful!'

            if current_biz.events.empty?
                redirect_to getsetup_url
            else
                redirect_to  account_summary_url
            end

        else
            flash[:notice] = 'Incorrect Email or Password.'
            redirect_to login_url   
        end 
    end

关于我在这里可能做错了什么的任何想法?

提前致谢!

4

1 回答 1

2

您也只需将 cookie 作为标头传递。例如:

BubbleWrap::HTTP.get("http://example.com/events.json", :headers=> {"Content-Type"=>'json', "Cookie" => @authCookie}) do |response|
  puts response.body #testing the response
end
于 2013-04-08T01:58:42.063 回答