Authorize Google Oauth2 - ruby-on-rails

I have an application ruby on rails and I'm trying to authorize my app in google, but I always get uninitialized constant
I tried to use Google::Auth::Stores::TokenStore and Google::APIClient::InstalledAppFlow
I followed theses samples https://github.com/google/google-auth-library-ruby#example-command-line and https://developers.google.com/youtube/v3/code_samples/ruby?hl=pt-br#upload_a_video
One use googleauth and other google-api-client.
In my Gemfile
gem 'google-api-client', '0.8.2', require: 'google/api_client'
gem 'googleauth'
In my code
Using first example
def authorization
client_id = Google::Auth::ClientId.from_file './client_secrets.json'
scope = ['SCOPE']
token_store = Google::Auth::Stores::FileTokenStore.new(file: './tokens.yaml')
authorizer = Google::Auth::UserAuthorizer.new(client_id, scope, token_store)
credentials = authorizer.get_credentials(user_id)
if credentials.nil?
url = authorizer.get_authorization_url(base_url: OOB_URI )
puts "Open #{url} in your browser and enter the resulting code:"
code = gets
credentials = authorizer.get_and_store_credentials_from_code(
user_id: user_id, code: code, base_url: OOB_URI)
end
end
Using second example
def authorization
file_storage = Google::APIClient::FileStore.new(oauth2)
if file_storage.authorization.nil?
client_secrets = Google::APIClient::ClientSecrets.load
flow = Google::APIClient::InstalledAppFlow.new(
client_id: client_secrets.client_id,
client_secret: client_secrets.client_secret,
scope: [YOUTUBE_UPLOAD_SCOPE]
)
client.authorization = flow.authorize(file_storage)
else
client.authorization = file_storage.authorization
end
end

I found the solution using the second case. When I added the gem gem 'google-api-client', '>0.7', require: 'google/api_client' in Gemfile I thought that all was done, but Google::APIClient::FileStorage and Google::APIClient::InstalledAppFlow need to require more files, so in the file you will use theses guys add these lines
require 'google/api_client/auth/file_storage'
require 'google/api_client/auth/installed_app'

Related

When attempting to retrieve Google Contacts via PeopleV1 API in Ruby Rails, why do I get "ArgumentError: unknown keyword: person_fields"?

I'm having a hard time integrating Google Contacts into my Ruby (version 2.2.10) on Rails 5 (version 5.1.5) App.
When I execute the following code block, I receive "ArgumentError: unknown keyword: person_fields".
people = Google::Apis::PeopleV1::PeopleService.new
people.authorization = auth_client
response = people.list_person_connections('people/me', page_size: 10,
person_fields: 'names,emailAddresses')
To rectify the problem, I tried using the following gem versions:
gem 'google-api-client', '~> 0.11'
gem 'google-api-client', '~> 0.8'
gem 'google-api-client'
I still receive the error no matter which version of the gem I use.
Below is the code in its entirety:
require 'google/apis/people_v1'
require 'google/api_client/client_secrets'
client_secrets = Google::APIClient::ClientSecrets.load 'client_secret_1088824912015-f8asojku302s0mvcijgj7takse8pg8rg.apps.googleusercontent.com.json'
auth_client = client_secrets.to_authorization
auth_client.update!( :scope => 'https://www.googleapis.com/auth/contacts.readonly', :redirect_uri => 'http://localhost:3000/oauth2callback', :additional_parameters => { "access_type" => "offline", "include_granted_scopes" => "true" } )
auth_uri = auth_client.authorization_uri.to_s
# To exchange an authorization code for an access token, use the fetch_access_token! method:
auth_client.code = #auth_code#
auth_client.fetch_access_token!
people = Google::Apis::PeopleV1::PeopleService.new
people.authorization = auth_client
response = people.list_person_connections('people/me', page_size: 10, person_fields: 'names,emailAddresses')
Any assistance would be greatly appreciated.
Just to clarify for any future readers, Ray Baxte linked to docs corresponding to version 0.9.19 of the google-api-client gem - the current version is 0.27.1
I'm using 0.26.0 and the correct argument is person_fields - this obviously changed somewhere between 0.9.19 and 0.26.0
I follow the documentation in the generated API method definitions:
https://github.com/googleapis/google-api-ruby-client/blob/0.27.1/generated/google/apis/people_v1/service.rb#L591
The correct attribute is fields not person_fields. See docs here.

Google Calendar API insert method not working on Ruby

I am trying to use Google Calendar API in my Ruby project and I meet a problem when I try to use the insert in the API. When I try the sample code on the https://developers.google.com/calendar/v3/reference/events/insert#examples,
I end up getting an error.
insertEvent.rb:2:in `<main>': uninitialized constant Google (NameError)
And if I paste this sample code after the quickstart.rb on this page
https://developers.google.com/calendar/quickstart/ruby, I will get this error:
quickstart.rb:84:in `<main>': undefined local variable or method `client' for main:Object (NameError)
Google did not get me the definition of client variable here so I do not what to insert here. I am a new learner of Ruby and thank you so much for helping.
Here is the code that I am stuck in
result = client.insert_event('primary', event)
puts "Event created: #{result.html_link}"
Ya, client isn't defined. Try this example code instead
https://github.com/googleapis/google-api-ruby-client/blob/master/samples/cli/lib/samples/calendar.rb
and
https://developers.google.com/calendar/quickstart/ruby
Important parts here:
require 'google/apis/calendar_v3'
require 'googleauth'
require 'googleauth/stores/file_token_store'
require 'fileutils'
OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'.freeze
APPLICATION_NAME = 'Google Calendar API Ruby Quickstart'.freeze
CREDENTIALS_PATH = 'credentials.json'.freeze
TOKEN_PATH = 'token.yaml'.freeze
SCOPE = Google::Apis::CalendarV3::AUTH_CALENDAR_READONLY
def authorize
client_id = Google::Auth::ClientId.from_file(CREDENTIALS_PATH)
token_store = Google::Auth::Stores::FileTokenStore.new(file: TOKEN_PATH)
authorizer = Google::Auth::UserAuthorizer.new(client_id, SCOPE, token_store)
# figure out your user_id
user_id = 'default'
credentials = authorizer.get_credentials(user_id)
if credentials.nil?
url = authorizer.get_authorization_url(base_url: OOB_URI)
puts 'Open the following URL in the browser and enter the ' \
"resulting code after authorization:\n" + url
code = gets
credentials = authorizer.get_and_store_credentials_from_code(
user_id: user_id, code: code, base_url: OOB_URI
)
end
credentials
end
calendar = Google::Apis::CalendarV3::CalendarService.new
calendar.client_options.application_name = APPLICATION_NAME
calendar.authorization = authorize
event = {
summary: 'Events',
attendees: [
{email: 'lpage#example.com'},
{email: 'sbrin#example.com'},
],
start: {
date_time: '2015-05-28T09:00:00-07:00',
time_zone: 'America/Los_Angeles',
},
end: {
date_time: '2015-05-28T17:00:00-07:00',
time_zone: 'America/Los_Angeles',
}
}
event = calendar.insert_event('primary', event, send_notifications: true)
and don't forget to run this first:
gem install google-api-client
and also to create these files with the correct authentication information
credentials.json
token.yaml
and to figure out your user_id.

How to access gmail api using ruby?

I am trying to access the gmail api for that I am using this rubygmail guide but everytime I am trying to run the code I am getting different errors I am using this code:
require 'google/apis/gmail_v1'
require 'googleauth'
require 'googleauth/stores/file_token_store'
require 'fileutils'
OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'
APPLICATION_NAME = 'Gmail API Ruby Quickstart'
CLIENT_SECRETS_PATH = 'client_secret.json'
CREDENTIALS_PATH = File.join(Dir.home, '.credentials',
"gmail-ruby-quickstart.yaml")
SCOPE = Google::Apis::GmailV1::AUTH_GMAIL_READONLY
##
# Ensure valid credentials, either by restoring from the saved credentials
# files or intitiating an OAuth2 authorization. If authorization is required,
# the user's default browser will be launched to approve the request.
#
# #return [Google::Auth::UserRefreshCredentials] OAuth2 credentials
def authorize
FileUtils.mkdir_p(File.dirname(CREDENTIALS_PATH))
client_id = Google::Auth::ClientId.from_file(CLIENT_SECRETS_PATH)
token_store = Google::Auth::Stores::FileTokenStore.new(:file => 'gmail-ruby-quickstart.yaml')
authorizer = Google::Auth::UserAuthorizer.new(
client_id, SCOPE, token_store)
user_id = 'me'
credentials = authorizer.get_credentials(user_id)
if credentials.nil?
url = authorizer.get_authorization_url(
base_url: OOB_URI)
puts "Open the following URL in the browser and enter the " +
"resulting code after authorization"
puts url
code = gets
credentials = authorizer.get_and_store_credentials_from_code(
user_id: user_id, code: code, base_url: OOB_URI)
end
credentials
end
# Initialize the API
service = Google::Apis::GmailV1::GmailService.new
service.client_options.application_name = APPLICATION_NAME
service.authorization = authorize
# Show the user's labels
user_id = 'me'
result = service.list_user_labels(user_id)
puts "Labels:"
puts "No labels found" if result.labels.empty?
result.labels.each { |label| puts "- #{label.name}" }
When I am running the ruby code I am getting this error:
nilay#nilay:~/gmail$ ruby quickstart.rb
/home/nilay/.rbenv/versions/2.3.1/lib/ruby/2.3.0/pstore.rb:414:in `load_data': PStore file seems to be corrupted. (PStore::Error)
from /home/nilay/.rbenv/versions/2.3.1/lib/ruby/2.3.0/pstore.rb:328:in `transaction'
from /home/nilay/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/googleauth-0.5.1/lib/googleauth/stores/file_token_store.rb:49:in `load'
from /home/nilay/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/googleauth-0.5.1/lib/googleauth/user_authorizer.rb:130:in `get_credentials'
from quickstart.rb:28:in `authorize'
from quickstart.rb:45:in `<main>'
How can I fix this please help me.
Use this gem
Gem gmail
Or Gem gmail-ruby-api

Problems understanding a Ruby example file for Google API Client Authentication

I am trying to access Google APIs from my Rails App.
I understand the basic flow for OAuth2.0 for Web Applications.
And I consider myself having a basic understanding of Ruby and Rails.
Yet, I can't make sense of the example Google shows in the "API Client Library for Ruby (Alpha)" Guides.
require 'google/apis/calendar_v3'
require 'google/api_client/client_secrets'
require 'sinatra'
require 'logger'
enable :sessions
def logger; settings.logger end
def calendar; settings.calendar; end
def user_credentials
# Build a per-request oauth credential based on token stored in session
# which allows us to use a shared API client.
#authorization ||= (
auth = settings.authorization.dup
auth.redirect_uri = to('/oauth2callback')
auth.update_token!(session)
auth
)
end
configure do
log_file = File.open('calendar.log', 'a+')
log_file.sync = true
logger = Logger.new(log_file)
logger.level = Logger::DEBUG
Google::Apis::ClientOptions.default.application_name = 'Ruby Calendar sample'
Google::Apis::ClientOptions.default.application_version = '1.0.0'
calendar_api = Google::Apis::CalendarV3::CalendarService.new
client_secrets = Google::APIClient::ClientSecrets.load
authorization = client_secrets.to_authorization
authorization.scope = 'https://www.googleapis.com/auth/calendar'
set :authorization, authorization
set :logger, logger
set :calendar, calendar_api
end
before do
# Ensure user has authorized the app
unless user_credentials.access_token || request.path_info =~ /^\/oauth2/
redirect to('/oauth2authorize')
end
end
after do
# Serialize the access/refresh token to the session and credential store.
session[:access_token] = user_credentials.access_token
session[:refresh_token] = user_credentials.refresh_token
session[:expires_in] = user_credentials.expires_in
session[:issued_at] = user_credentials.issued_at
end
get '/oauth2authorize' do
# Request authorization
redirect user_credentials.authorization_uri.to_s, 303
end
get '/oauth2callback' do
# Exchange token
user_credentials.code = params[:code] if params[:code]
user_credentials.fetch_access_token!
redirect to('/')
end
get '/' do
# Fetch list of events on the user's default calandar
events = calendar.list_events('primary', options: { authorization: user_credentials })
[200, {'Content-Type' => 'application/json'}, events.to_h.to_json]
end
Of course this looks like Ruby, but I never saw some of the constructs used here, and I am not sure where in a Rails app that code would go.
Is it Ruby? If so, how do I use it?

How do I authenticate users in a Rails app with the oauth gem and twitter 1.0.0 gem?

Jnunemaker just updated his twitter gem (https://github.com/jnunemaker/twitter) and removed the Twitter::Oauth class. My code doesn't look much like his example, so I'm having issues updating it. Here's what my code used to look with the twitter 0.9 gem:
UsersController
def oauth
consumer = Twitter::OAuth.new('mykey','mysecret')
request_token = consumer.request_token
session[:request_token] = request_token.token
session[:request_token_secret] = request_token.secret
redirect_to 'http://api.twitter.com/oauth/authorize?oauth_token='+request_token.token
end
def callback
consumer = Twitter::OAuth.new('mykey','mysecret')
atoken, asecret = oauth.authorize_from_request(session[:request_token], session[:request_token_secret], params[:oauth_verifier])
consumer.authorize_from_access(atoken,asecret)
user = Twitter::Base.new(consumer).verify_credentials
#and then I create a new user in my application, with attributes such as the user's follower count, etc
end
Here's an example of what I've tried to do to change this code:
UsersController
def oauth
consumer = OAuth::Consumer.new("mykey", "mysecret", :site => "siteurl")
request_token = consumer.get_request_token
session[:request_token] = request_token.token
session[:request_token_secret] = request_token.secret
redirect_to 'http://api.twitter.com/oauth/authorize?oauth_token='+request_token.token
end
def callback
consumer = OAuth::Consumer.new("mykey", "mysecret", :site => "siteurl")
request_token = session[:request_token]
atoken = OAuth::RequestToken.new(consumer, request_token.token, request_token.secret).get_access_token(:oauth_verifier => params[:oauth_verifier])
consumer.authorize_from_access(atoken)
user = Twitter::Client.new(consumer).verify_credentials
Gemfile
...
gem 'oauth'
I'm sure there are a number of things wrong in my callback method, but one thing that's weird is that my oauth method works fine when I'm running locally, but gives me a '502 Bad Gateway' error when I try from my live (deployed with heroku) version.
If you can't get it to work with what you have now, I have been able to use the Omniauth gem together with the Twitter gem. Omniauth is very easy to setup.
To use the Twitter gem, just get the access token info after the Omniauth callback is done:
token = omniauth['credentials']['token'],
secret = omniauth['credentials']['secret']
Then just set the Twitter gem config settings before using the Twitter gem methods
Twitter.oauth_token = token
Twitter.oauth_token_secret = secret
Twitter.home_timeline.first.text
(You'll have to configure the Twitter gem consumer_key and consumer_key_secret if you don't haven't that already set up in an initializer file...)
I've had good luck with
Authlogic + AuthLogic Connect.
I'm not sure if you need to implement the oauth by hand, but the gem might be worth looking into.
https://github.com/viatropos/authlogic-connect
The only gotcha I've found with oauth providers is sometimes they provide poor error messages if the callback url isn't recognized, which is configured where you get the api keys.
-Ken
You were close in your example. The right code for your controller action would be something like this:
def new
consumer = OAuth::Consumer.new(YOUR_CONSUMER_TOKEN, YOUR_CONSUMER_SECRET, site: 'https://api.twitter.com', request_endpoint: 'https://api.twitter.com', authorize_path: '/oauth/authenticate')
unless params[:oauth_token]
request_token = consumer.get_request_token({ oauth_callback: request.original_url })
session[:request_token] = { token: request_token.token, secret: request_token.secret}
redirect_to request_token.authorize_url(force_login: 'true')
else
request_token = OAuth::RequestToken.from_hash(consumer, oauth_token: session[:request_token]["token"], oauth_token_secret: session[:request_token]["secret"])
access_token = request_token.get_access_token(oauth_verifier: params[:oauth_verifier])
session[:request_token] = nil
#client = Twitter::REST::Client.new do |config|
config.consumer_key = YOUR_CONSUMER_TOKEN
config.consumer_secret = YOUR_CONSUMER_SECRET
config.access_token = access_token.token
config.access_token_secret = access_token.secret
end
end
end

Resources