Site url getting overridden using rails oauth2 plugin - ruby-on-rails

I am using ruby oauth2 gem
I have my site URL as
site = "https://192.168.5.15:9443/oauth2/authorize"
but once i call
client.auth_code.authorize_url(:redirect_uri => redirect_uri)
My URL is changed to
https://192.168.5.15:9443/oauth/authorize?client_id=J7H_LoEIdaf9aVXF_opqtVMLgwoa&redirect_uri
So my oauth2/authorize is being replaced by oauth/authorize
Is there any way to fix this?
Thank you in advance.

You need to pass the :authorize_url attribute.
require 'oauth2'
client = OAuth2::Client.new('client_id', 'client_secret', :authorize_url => '/oauth2/authorize', :site => 'https://192.168.5.15:9443')

Related

rails login facebook api debug

I'm using devise and omniauth-facebook authentication in my rails application.
Facebook has moved on to v2.x graph API. (deadline for api migration is April 30, 2015).
I configure my omniauth-facebook in initializer file as:
provider :facebook, ENV['APP_ID'], ENV['APP_SECRET'],
:scope => 'email,read_stream',
:client_options => {
:site => 'https://graph.facebook.com/v2.0',
:authorize_url => "https://www.facebook.com/v2.0/dialog/oauth"
}
But...how I can know that the calls are actually doing in the 2.x api version and not the 1.x I had before?.
Thanks
You could add the gem http_logger to your gemfile. You will then see the server-side http requests in your log:
[DEBUG] HTTP POST (123.07ms) https://graph.facebook.com:443/v2.0/oauth/access_token

Ruby on Rails 4: Extract Oauth2 parameters from token response?

I am developing a rails application which uses oauth2 gem to Authenticate a User against Windows azure AD .
I am able to authenticate the user with Azure AD ,but my problem is i need to extract the parameters inside token response.
#token = client.auth_code.get_token(params[:code], :redirect_uri => "#{APP_URL}/callback", :resource => "#{RES_URL}")
I tried puts #token in controller but i am getting some Hash values.
#<OAuth2::AccessToken:0x000000030ed960>
Is it possible to see the contents inside #token.
And also can i set headers inside token request.
#token = client.auth_code.get_token(params[:code], :redirect_uri => "#{APP_URL}/callback", :resource => "#{RES_URL}",:headers => {'Content-Type' => 'application/json' })
Any help is appreciated.
Yo can try to use puts #token.to_yml
to see the contents of #token

How do I use GET and POST on Github using access tokens?

I am using OmniAuth to authenticate a user via Github. OmniAuth provides access tokens. Now I want to send the GET or POST request to Github. I don't want to use any gems, I want to do with Net::HTTP. I did it like this:
<%consumer = OAuth::Consumer.new("mshsD0jpgcYwwOEcTW5ZTA", "V6KTqllY5jS392pj4FNFCb5EiOM8DaFzVwr9cS54XQ", { :site => "https://api.github.com", :request_token_path => '/oauth/request_token', :access_token_path => '/oauth/access_token', :authorize_path => '/oauth/authorize', :scheme => :header })%>
<%access_token = OAuth::AccessToken.new(consumer,auth.token,auth.secret)%>
The same I previously did for Twitter worked fine but now I am getting the following error:
uninitialized constant ActionView::CompiledTemplates::OAuth
Even in the same application the same thing is working for Twitter but not for Github.
I searched through Google but found nothing that helped.
You should be using OAuth2 instead of OAuth. I'd actually recommend using Octokit, it's easy to use and Wynn works for GitHub now so part of his job is keeping it up to date. :)
If you want to use Net::HTTP (although I can't imagine why), you can actually do that without any gems. Just put the token you got from OmniAuth in the request's "Authentication" header.
require 'net/https'
require 'uri'
uri = uri = URI.parse("https://api.github.com/users/username")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
headers = { "Authentication" => "token" }
request = Net::HTTP::Get.new(uri.request_uri, headers)
response = http.request(request)
response.body # => A string containing the JSON response
Seeing as you're already using Omniauth and are familiar with it, I'd recommend using the omniauth-github strategy: https://github.com/intridea/omniauth-github

how to get oauth access token for facebook using ruby

Am trying to get oauth access token for facebook programmatically in ruby.
My code is as follows:
client = OAuth2::Client.new(
APP_ID,
SECRET_ID,
:authorize_url => "/dialog/oauth",
:token_url => "/oauth/access_token",
:site => "https://www.facebook.com/"
)
code = client.auth_code.authorize_url(:redirect_uri => "http://www.facebook.com/")
token = client.auth_code.get_token(code, :redirect_uri => "https://graph.facebook.com/")
OAuth2::AccessToken.new(client, token.token, {:mode => :query, :param_name =>"oauth_token"})
When i try to run the above ruby code, i'm getting the following exception
https://www.facebook.com/dialog/oauth?response_type=code&client_id=APP_ID
51&redirect_uri=http%3A%2F%2Fwww.facebook.com%2F
/home/ec2-user/.rvm/gems/ruby-1.9.3-p0#samples/gems/oauth2-0.5.2/lib/oauth2/clie
nt.rb:129:in `get_token': OAuth2::Error (OAuth2::Error)
from /home/ec2-user/.rvm/gems/ruby-1.9.3-p0#samples/gems/oauth2-0.5.2/li
b/oauth2/strategy/auth_code.rb:29:in `get_token'
from oauth.rb:16:in `<main>'
Any help is greatly appreciated as I have spent more than a day while trying to sort this out.
Have you tried to put as redirect_uri instead of localhost:3000 your real IP address ex. 231.61.233.57:3000? Additionally you could try to use ssh tunneling for testing purposes so your localhost application will be available worldwide. Check this out http://progrium.com/localtunnel/ .
When you will get ip address from this tool try to set redirect_uri param to it.

Ruby and Rails - oauth and http proxy

Does anybody know how to implement an HTTP PROXY with oauth for rails?
I'm using the oauth gem but am behind a proxy server.
Finding it very difficult to work this out.
Very frustrating!
Thanks for any help,
John
Try adding the :proxy property when you create your consumer:
#consumer = OAuth::Consumer.new( consumer_key, consumer_secret, {
:site => 'http://site.com',
:proxy => 'http://proxy.mycompany.com:8080',
:request_token_path => "/oauth/request_token",
:access_token_path => "/oauth/access_token",
:authorize_path => "/oauth/authorize"
})
Solution NOT tested
Did you try to set the OAuth::Consumer.proxy = http://login:password#ip_address/
You can do this on RequestToken.consumer.proxy or even on AccessToken.consumer.proxy
It should from what I've seen in the ruby library.
Good luck

Resources