Ruby and Rails - oauth and http proxy - ruby-on-rails

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

Related

How can I authorize Google API Client for a service account with ENV variables?

I am trying to authorize the Calendar API for my app. I can't get the authorization to work with the supplied .json file or by using ENV variables.
Can someone please explain to me how to do this? I would prefer to use ENV variables, but if that's not possible, then any method that works will be amazing!
This is what I have now:
client = Google::APIClient.new(:application_name => 'App Name', :application_version => '1.0.0')
client_secrets = Google::APIClient::ClientSecrets.load
client.authorization = Signet::OAuth2::Client.new(
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
:audience => 'https://accounts.google.com/o/oauth2/token',
:scope => 'https://www.googleapis.com/auth/calendar',
:issuer => Rails.application.secrets.GOOGLE_CLIENT_EMAIL,
:person => Rails.application.secrets.GOOGLE_CALENDAR_EMAIL
)
client.authorization.fetch_access_token!
service = client.discovered_api('calendar', 'v3')
You can use figaro to create environmental variable in rails. Checkout their docs for more configuration options.

Site url getting overridden using rails oauth2 plugin

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')

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.

Devise and google oauth. (ror3)

Is it possible to use devise and google oauth together?
I have successfully setup facebook in devise, but the following
google config doesn't work. Do I have to use oauth2 directly?
config.oauth :google, 'anonymous', 'anonymous',
:site => 'https://www.google.com',
:request_token_path => "/accounts/OAuthGetRequestToken",
:access_token_path => "/accounts/OAuthGetAccessToken",
:authorize_path => "/accounts/OAuthGetAuthorizeToken",
:signature_method => "RSA-SHA1",
:private_key_file => '/rsakey.pem',
:scope => "https://www.google.com/m8/feeds/"
Ok my mistake. Google uses oauth whereas devise supports only oauth2 and they are not backwards compatible as far I understand. So I have to use my own implementation.

Resources