How to use basic authentication with httparty in a Rails app?
auth = {:username => "test", :password => "test"}
@blah = HTTParty.get("http://twitter.com/statuses/public_timeline.json",
:basic_auth => auth)
Doing a Http basic authentication in rails
Write the below code, in the controller which you want to restrict using http basic authentication
class ApplicationController < ActionController::Base
http_basic_authenticate_with :name => "user", :password => "password"
end
Making a request with open-uri would look like this:
require 'open-uri'
open("http://www.your-website.net/",
http_basic_authentication: ["user", "password"])
How can I use Digest and Basic authentications with HTTParty in the same class?
When you use basic_auth
or digest_auth
, HTTParty stores the information internally in the @default_options
hash. Here is the source for basic_auth
:
# File 'lib/httparty.rb', line 102
def basic_auth(u, p)
default_options[:basic_auth] = {:username => u, :password => p}
end
You can get access to that hash using the default_options
method:
# File 'lib/httparty.rb', line 452
def default_options #:nodoc:
@default_options
end
I'd try:
default_options.delete(:basic_auth)
or:
default_options.delete(:digest_auth)
prior to using the other authentication method.
This is untested code but looks 'bout right:
class Test
include HTTParty
debug_output $stdout
def first_request(href)
klass = self.class
klass.base_uri "SERVER:PORT"
klass.default_options.delete(:basic_auth)
klass.digest_auth 'login', 'pass'
klass.get(href, {:query => {}})
end
def second_request(href)
klass = self.class
klass.default_options.delete(:digest_auth)
klass.post(
'',
{
:body => xml_string,
:basic_auth => {
:username => "USERNAME",
:password => "PASSWORD"
}
}
)
end
end
RestClient/HTTParty authentication parameters not being used
I can't understand your saying Works but is wrong but Charity Navigator Data API request the sending app_id
and app_key
keys through the parameters, not header.
Your first code looks correct.
Second code, app_key
key was sent by header, not params. So API response 403.
Third code, coded with httpart gem, dose not use parameters but headers. So Charity Navigator Data API response Authentication parameters missing
error. It's normal.
require 'httparty'
class StackExchange
include HTTParty
base_uri 'https://api.data.charitynavigator.org/v2/'
def posts
options = {
query: {
app_id: '2b1ffdad',
app_key: 'XXXX'
}
}
self.class.get("/Organizations/", options)
end
end
charity = Charity.new
puts charity.posts
But you can use parameters by query option
in httparty. Read the httparty docs and this SO. And you can use parameters options in rest-client gem, too. And I recommend use it strongly.
HTTParty Digest Auth
you can set the username and password using the digest_auth
method when defining your class
class Foo
include HTTParty
digest_auth 'username', 'password'
end
Related Topics
Convert String with Comma to Integer
How to Install Ruby 1.9.3 on Ubuntu Without Rvm
Normalizing Line Endings in Ruby
What Happens When Modifying Gemfile.Lock Directly
Why Do I Get an "Undefined Method for 'Has_Attached_File' When Installing Paperclip
Use Global or Constant Variable in Ruby/Rails
Trouble Resizing the Default Image with Paperclip
Error Installing a Pod - Bus Error at 0X00000001045B8000
Heroku - How to Write into "Tmp" Directory
Ruby: Titleize: How to Ignore Smaller Words Like 'And', 'The', 'Or, etc
How to Use Basic Authentication with Httparty in a Rails App
Select Mailbox "Sent Mail" or "All Mail" in Ruby Net::Imap
Rails Can't Login to Postgresql - Pg::Error - Password - Correct Info
How to Get Ruby to Parse Time as If It Were in a Different Time Zone
Failing to Install Nokogiri Gem
Url_For of a Custom Restful Resource (Composite Key; Not Just Id)