简体   繁体   English

机械化:未登录站点

[英]Mechanize: not logged in the site

I'm trying to authorise to other site with gem mechanize 我正在尝试使用gem 机械化授权到其他站点

My code: 我的代码:

  def login_zenit
    agent = Mechanize.new
    agent.get('http://mobile.zenitbet.com/') do |p|
      f = p.forms.first
      f.login = 'login'
      f.fields[1].value = 'password'
      f.submit
    end
    agent.get('http://mobile.zenitbet.com/')
    redirect_to root_url
  end

The problem is when I run login_zenit it doesn't work - I'm not authorising to the site in web browser. 问题是当我运行login_zenit时不起作用-我未授权使用Web浏览器访问该站点。 Although if I run this code in rails console it works perfectly. 尽管如果我在rails console运行此代码,它会完美运行。 Where did I make a mistake? 我在哪里弄错了? maybe there is a problem with redirect_to root_url ? 也许redirect_to root_url有问题?

Thanks! 谢谢!

In general is not possible. 通常是不可能的。 For this to work visiting your app would need to set the appropriate cookies for mobile.zenibet.com. 为此,访问您的应用程序需要为mobile.zenibet.com设置适当的cookie。

You do have those cookie values - they're inside the mechanize object, but even if you were to extract them you wouldn't be able to set them on the correct domain. 您确实具有这些cookie值-它们位于机械化对象内,但是即使要提取它们,也无法在正确的域上设置它们。 If your app is being served from foo.com then the browser will let you set cookies on foo.com or any subdomain of it, but it won't let you set cookies on another arbitrary domain (see point 5 in section 5.3 of the rfc ) 如果您的应用是从foo.com提供服务,则浏览器将允许您在foo.com或其任何子域上设置cookie,但不允许您在另一个任意域上设置cookie(请参见5.3.3节的第5点)。 射频

Unless your app runs on a subdomain of zenibet.com I think you are out of luck 除非您的应用程序在zenibet.com的子域上运行,否则我认为您不走运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM