Thursday, November 18, 2010

Digest Authentication

hi,

You all know about basic authentication method use in most places here I share something about digest authentication method.

Below code is for digest authentication ask using pop-up when try to enter site.

Installation:

If you already have installed ruby gem then you have to now install one more gem for
digest authentication gem.

gem install net-http-digest_auth

check http://rubygems.org/gems/net-http-digest_auth for dependency for this gem.

Example code :

require 'rubygems'
require 'uri'
require 'net/http'
require 'net/http/digest_auth'


def authentication_check

#change your site which have digest authentication, this url use just for example.
uri = URI.parse 'http://api.del.icio.us/v1/tags/get'
uri.user = 'username'
uri.password = 'password'

digest_auth = Net::HTTP::DigestAuth.new
auth = digest_auth.auth_header uri, res['www-authenticate'], 'GET'
req = Net::HTTP::Get.new uri.request_uri
req.add_field 'Authorization', auth
res = h.request req
#return res.code # for get http return code
return res.message # for http message

end


Thanks,
Priyank Shah

No comments:

Post a Comment