Todd A. Jacobs wrote:
I'm trying to get basic authentication to work with Ruby 1.8 and
WWW::Mechanize 0.7, and am having no luck:
require 'mechanize'
agent=WWW::Mechanize.new
agent.auth('foo', 'bar')
agent.get('http://192.168.20.10')
I get the following error:
WWW::Mechanize::ResponseCodeError: 401 => Net::HTTPUnauthorized
Yes, I'm using the correct username and password. I can log in properly
via Firefox, just not with mechanize. How do I get basic authentication
to work in this instance?
Is it solved? Im having the exactly same problem as well. I have tried
basic_auth, but didnt work. while curl -u user:pass (URL) works.
my env = ubuntu 10.04 x64, ruby1.9.1, mechanize 1.0.0
code =
agent = WWW::Mechanize.new
agent.log = Logger.new(STDOUT)
agent.auth(user, pass)
agent.get
"http://api.t.sina.com.cn/statuses/friends_timeline.#{RespFormat}?source=#{APIKEY\}"
logs =
I, [2010-09-13T12:48:21.213607 #3950] INFO -- : Net::HTTP::Get:
/statuses/friends_timeline.xml?source=2485033351
D, [2010-09-13T12:48:21.213892 #3950] DEBUG -- : request-header:
accept-language => en-us,en;q=0.5
D, [2010-09-13T12:48:21.214037 #3950] DEBUG -- : request-header: accept
=> */*
D, [2010-09-13T12:48:21.214164 #3950] DEBUG -- : request-header:
user-agent => WWW-Mechanize/0.9.3
(http://rubyforge.org/projects/mechanize/\)
D, [2010-09-13T12:48:21.214291 #3950] DEBUG -- : request-header:
connection => keep-alive
D, [2010-09-13T12:48:21.214414 #3950] DEBUG -- : request-header:
accept-encoding => gzip,identity
D, [2010-09-13T12:48:21.214553 #3950] DEBUG -- : request-header: host =>
api.t.sina.com.cn
D, [2010-09-13T12:48:21.214676 #3950] DEBUG -- : request-header:
accept-charset => ISO-8859-1,utf-8;q=0.7,*;q=0.7
D, [2010-09-13T12:48:21.214800 #3950] DEBUG -- : request-header:
keep-alive => 300
D, [2010-09-13T12:48:21.249003 #3950] DEBUG -- : Read 0 bytes
D, [2010-09-13T12:48:21.249511 #3950] DEBUG -- : Read 161 bytes
D, [2010-09-13T12:48:21.249607 #3950] DEBUG -- : response-header:
x-varnish => 483890686
D, [2010-09-13T12:48:21.249650 #3950] DEBUG -- : response-header:
expires => Thu, 01 Jan 1970 00:00:00 GMT
D, [2010-09-13T12:48:21.249691 #3950] DEBUG -- : response-header:
connection => keep-alive
D, [2010-09-13T12:48:21.249732 #3950] DEBUG -- : response-header: via =>
1.1 varnish
D, [2010-09-13T12:48:21.249772 #3950] DEBUG -- : response-header:
content-type => application/xml;charset=UTF-8
D, [2010-09-13T12:48:21.249813 #3950] DEBUG -- : response-header: date
=> Mon, 13 Sep 2010 04:47:23 GMT
D, [2010-09-13T12:48:21.249854 #3950] DEBUG -- : response-header: server
=> weibo
D, [2010-09-13T12:48:21.249892 #3950] DEBUG -- : response-header:
content-length => 161
D, [2010-09-13T12:48:21.249933 #3950] DEBUG -- : response-header:
accept-ranges => none
D, [2010-09-13T12:48:21.249973 #3950] DEBUG -- : response-header: age =>
0
D, [2010-09-13T12:48:21.250013 #3950] DEBUG -- : response-header:
cache-control => no-cache
D, [2010-09-13T12:48:21.250054 #3950] DEBUG -- : response-header: pragma
=> No-cache
I, [2010-09-13T12:48:21.250260 #3950] INFO -- : status: 403
/usr/lib/ruby/1.8/www/mechanize.rb:238:in `get': 403 =>
Net::HTTPForbidden (WWW::Mechanize::ResponseCodeError)
from ./sinaModel.rb:39:in `get_friends_timeline'
from test.rb:6
···
--
Posted via http://www.ruby-forum.com/\.