WWW::Mechanize and basic_auth

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?

Hi Todd.

“WWW::Mechanize#auth” is not basic authentication method.
Please try again by using “WWW::Mechanize#basic_auth” for basic
authentication.

require ‘mechanize’
agent = WWW::Mechanize.new
agent.basic_auth(‘foo’, ‘bar’)
agent.get(‘http://192.168.20.10http://192.168.20.10’/)

2008/3/16, Todd A. Jacobs [email protected]:

On Sun, Mar 16, 2008 at 12:27:21PM +0900, saronpasu wrote:

“WWW::Mechanize#auth” is not basic authentication method. Please try
again by using “WWW::Mechanize#basic_auth” for basic authentication.

I get exactly the same result with this method as well.

On Sun, Mar 16, 2008 at 11:10 PM, Todd A. Jacobs <
[email protected]> wrote:

On Sun, Mar 16, 2008 at 12:27:21PM +0900, saronpasu wrote:

“WWW::Mechanize#auth” is not basic authentication method. Please try
again by using “WWW::Mechanize#basic_auth” for basic authentication.

I get exactly the same result with this method as well.

it’s username first, then password. Have u gotten the order mixed up?

-jf


In the meantime, here is your PSA:
“It’s so hard to write a graphics driver that open-sourcing it would not
help.”
– Andrew Fear, Software Product Manager, NVIDIA Corporation

On Mon, Mar 17, 2008 at 02:23:00AM +0900, Jeffrey ‘jf’ Lim wrote:

it’s username first, then password. Have u gotten the order mixed up?

Nope.

On Sun, Mar 16, 2008 at 06:05:04AM +0900, 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')

It occurs to me that there is no documented interface for selecting a
realm. Is this automagical, or am I supposed to set the realm somehow?
If so, how do I do that, and where is it documented?

On Tue, Mar 18, 2008 at 02:56:06AM +0900, Todd A. Jacobs wrote:

It occurs to me that there is no documented interface for selecting a
realm. Is this automagical, or am I supposed to set the realm somehow?
If so, how do I do that, and where is it documented?

For basic authentication, mechanize just delegates to net/http. Can you
add a logger to mechanize and send me your logs?

agent.log = Logger.new(STDOUT)

Is this against Apache? If so, could you send your config, and I’ll try
to reproduce the error.

On Tue, Mar 18, 2008 at 1:56 AM, Todd A. Jacobs <
[email protected]> wrote:

It occurs to me that there is no documented interface for selecting a
realm. Is this automagical, or am I supposed to set the realm somehow?
If so, how do I do that, and where is it documented?

the realm is presented by the server. This has nothing to do with the
client
in that sense.

-jf


In the meantime, here is your PSA:
“It’s so hard to write a graphics driver that open-sourcing it would not
help.”
– Andrew Fear, Software Product Manager, NVIDIA Corporation

Yiling C. wrote:

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.

Check the whole thread at WWW::Mechanize and basic_auth - Ruby - Ruby-Forum

It says you need agent.basic_auth(…), not agent.auth(…)

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

Hi, Thanks for the reply, appreciated it!

I did have tried basic_auth() as i did read the whole thread.

In fact in manual it says basic_auth() is an alias of auth() according
to http://mechanize.rubyforge.org/mechanize/Mechanize.html#M000229

Problem is mechanize or(net/http) is not sending the HTTP username and
password to the server, resulting server throws back 403 Forbidden
Error.

Now is it the bug on all requests are this is occasionally happening?

Hi,

On Mon, Sep 13, 2010 at 1:22 PM, Yiling C. [email protected]
wrote:

Now is it the bug on all requests are this is occasionally happening?

Mechanize has unit tests around its basic_auth functionality:

http://github.com/tenderlove/mechanize/blob/master/test/test_authenticate.rb#L9

If you’ve got a reproducible case that you can either put into
executable
code, or at least describe in detail, then please report the bug to the
mechanize mailing list for feedback:

http://rubyforge.org/mailman/listinfo/mechanize-users

Thanks.