Mechanize authentication error 401 when saving file to a shared drive

39 Views Asked by At

I am connected to a shared SMB volume. Using Mechanize in a Ruby script, I am trying to download several files from a proprietary asset manager, and save them directly to the shared drive, i.e.:

agent = Mechanize.new
agent.add_auth("http://website.com", username, password)
agent.get(URL).save("/Volumes/shared_folder/file.pdf")

This works successfully for a few iterations/files, and then eventually throws a 401 error each time:

/Users/m/.rvm/rubies/ruby-2.4.6/lib/ruby/gems/2.4.0/gems/mechanize-2.7.6/lib/mechanize/http/agent.rb:775:in `response_authenticate': 401 => Net::HTTPUnauthorized for [url] -- NTLM authentication failed -- available realms:  (Mechanize::UnauthorizedError)
    from /Users/m/.rvm/rubies/ruby-2.4.6/lib/ruby/gems/2.4.0/gems/mechanize-2.7.6/lib/mechanize/http/agent.rb:323:in `fetch'
    from /Users/m/.rvm/rubies/ruby-2.4.6/lib/ruby/gems/2.4.0/gems/mechanize-2.7.6/lib/mechanize.rb:464:in `get'

However, if I change it so that the files are saved locally, in the same working folder that I am running my Ruby script, I get no errors at all.

The credentials for the website and for the shared drive are not the same, so I'm not sure if that has anything to do with it.

Is there any way around this that does not involve saving files directly to my computer?

Edit:

If I add a rescue clause like this, the script is able to finish its execution:

rescue Mechanize::UnauthorizedError => e
agent = Mechanize.new
agent.add_auth("http://website.com", username, password)
retry

Although this works, it is triggered every 1-3 iterations, and it overall just feels like a clunky solution.

I also tried setting the timeout with agent.read_timeout, but I don't necessarily know how long each iteration would actually take, so I don't have a concrete value to define.

0

There are 0 best solutions below