Set authentication token in http header

I am following railscast on how to set authentication tokens http://railscasts.com/episodes/352-securing-an-api?view=asciicast

I have a very well configured application and it uses the authenticate_or_request_with_http_token method to get the token.

My problem is that I have the following application which should set the token in the header. Sort of:

uri = URI.parse(full_url)
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
request['HTTP_AUTHORIZATION'] = 'this_is_a_test_key'
response = http.request(request)

The code above is denied access. I know it is easy to set custom ones like X-CUSTOM-TOKEN, but how to set the default value?

+4
source share
3 answers

HTTP_AUTHORIZATION, , :

request['authorization'] = "Token token=#{token}"

authenticate_or_request_with_http_token.

+11

ActionController:: HttpAuthentication,

user = 'whatever'
pass = 'you-like'
auth = ActionController::HttpAuthentication::Basic.encode_credentials(user, pass)
request.headers['Authorization'] = auth

,

token = 'whatever-it-is'
auth = ActionController::HttpAuthentication::Token.encode_credentials(token)
request.headers['Authorization'] = auth
+1

The accepted answer does not work.

In Rails 4, this should request.authorizationnot berequest['authorization']

0
source

All Articles