Devise and google oauth. (ror3) - ruby-on-rails

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.

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

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

How to pull Google Analytics stats?

Is Google API Ruby client the best option?
I have a site example.com with users and I want them to see their google analytics stats on example.com, how can I do it ?
I can see the example but I'm not able to figure out how to begin.
I also use the google-api-ruby-client gem and set it up about the same way that is outlined in the link you provided (https://gist.github.com/joost/5344705).
Just follow the steps outlined in the link to set up a Google Analytics client:
# you need to set this according to your situation/needs
SERVICE_ACCOUNT_EMAIL_ADDRESS = '...' # looks like 12345#developer.gserviceaccount.com
PATH_TO_KEY_FILE = '...' # the path to the downloaded .p12 key file
PROFILE = '...' # your GA profile id, looks like 'ga:12345'
require 'google/api_client'
# set up a client instance
client = Google::APIClient.new
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/analytics.readonly',
:issuer => SERVICE_ACCOUNT_EMAIL_ADDRESS,
:signing_key => Google::APIClient::PKCS12.load_key(PATH_TO_KEY_FILE, 'notasecret')
).tap { |auth| auth.fetch_access_token! }
api_method = client.discovered_api('analytics','v3').data.ga.get
# make queries
result = client.execute(:api_method => api_method, :parameters => {
'ids' => PROFILE,
'start-date' => Date.new(1970,1,1).to_s,
'end-date' => Date.today.to_s,
'dimensions' => 'ga:pagePath',
'metrics' => 'ga:pageviews',
'filters' => 'ga:pagePath==/url/to/user'
})
puts result.data.rows.inspect
To display statistics for a user's page in your app, you have to adjust the metrics and filters parameters when making the query. The query above for example will return a result object containing all pageviews for the page with url example.com/url/to/user.
Caveat: this answer was written a long time ago and Google released a new, incompatible version of the gem. Please consult https://github.com/google/google-api-ruby-client/blob/master/MIGRATING.md

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