使用Mechanize gem登录

时间:2013-06-03 06:11:09

标签: ruby authentication mechanize

My code is this


 require 'mechanize'
  obj=Mechanize.new
  a.get("http://cafedepoca.com/submit_data?name=test&email=test@mail.com&pass=mypassword&prof_type=30&api_key=mykey") do |d|
  d.user='testuser'
  d.password='mypassword'
  end.click_button

它返回并且 error.Net::HTTPUnauthorized 。如果在弹出框中填写凭证的任何其他方式。如果存在任何其他宝石,建议我。

2 个答案:

答案 0 :(得分:2)

我检查了http://cafedepoca.com,并提示使用BasicAuthentication。你必须先输入它。

 require 'mechanize'
 a=Mechanize.new
 a.auth('username','password') # fix this line with correct login and password for BasicAuth  
 a.get("http://cafedepoca.com/submit_data?name=test&email=test@mail.com&pass=mypassword&prof_type=30&api_key=mykey").click_button

它应该以这种方式工作。

答案 1 :(得分:2)

您需要使用Mechanize#authMechanize#add_auth来传递基本身份验证。

require 'mechanize'

USERNAME = "XXXXXXXX"
PASSWORD = "YYYYYYYY"

agent = Mechanize.new
agent.auth(USERNAME, PASSWORD)
agent.get("http://cafedepoca.com/submit_data?name=test&email=test@mail.com&pass=mypassword&prof_type=30&api_key=mykey")
相关问题