Get Google's authentication_token in the Ruby on Rails application - ruby-on-rails

I want to use the Google API in my Rails application. For this, I need to authenticate to a Google Dev account to get the authentication_token.
The best way to do it, in my opinion, is to use oauth2 library and examples provided in the documentation.
As you see, I have tried to use Google::APIClient in there, although I am stuck with the following issue below
In the console I get:
Google::APIClient::KeyUtils.load_from_pkcs12('file.p12', 'notasecret')
NameError: uninitialized constant Google
I'm using following gems:
gem 'oauth2'
gem 'omniauth'
gem 'omniauth-google-oauth2'
gem 'google-api-client'
Any suggestions?

You just need to require a file
require 'google/api_client/auth/key_utils'
Only then you would be allowed calling a Google::APIClient::KeyUtils.
You can notice in the documentation they require each class/module separately.
It seems like gem does not load all of its dependencies itself, because of their ideology and just not to overload the application with redundant libraries that won't be used.
Take a note, it is quite possible you will need to require some other libraries.

Related

What does `require:` mean in a Rails gemfile?

I'm doing a tutorial on authentication and came across the following line for gemfile. What is the use of require here?
gem 'google-api-client', require: 'google/api_client'
Tutorial: http://willschenk.com/setting-up-devise-with-twitter-and-facebook-and-other-omniauth-schemes-without-email-addresses/
I understand require in Javascript, but in Rails I thought the gemfile is for installing gems, and once they are installed they can be used in the application, and thats all there is to it... so I'm not sure why I would use require.
I'm particularely interested because after adding this line and starting the server, I ran into an error.
Error:
/usr/local/rvm/gems/ruby-2.3.0/gems/bundler-1.11.2/lib/bundler/runtime.rb:77:in
`require': cannot load such file -- google/api_client (LoadError)
Temporary solution: I've commented out the require: part and the error is prevented. But maybe this is not ideal.
So understanding the use of require would help very much in troubleshooting this.
I read other articles on SO, but they discuss specifics like require => nil and require => false, which I think is a little different from my question.
Bunder: What does :require => nil in Gemfile mean?
Bundler: What does :require => false in a Gemfile mean?
Can anyone share some incite?
UPDATE
I later found this which explains it well: When do you need a require in a rails Gemfile?
If you omit the :require option, by default Bundler will attempt to
require the gem by using the standard name-to-file conversion rule:
This works well if the gem author has followed the standard
conventions. But in some cases, for a variety of reasons, this doesn't
happen.
When the gem itself doesn't require any of its lib, you need to do that either in your Gemfile (the way you wrote) or in some file in your project.
Eg.: imagine a gem which has more than one solution for any particular problem. However, you don't want to load all of those solutions (files), you only need one. Then you'd need to specify which file you want to load by using require: some_lib.

Unitinialized constant. Integrating Google API in Rails

I am having a trouble integrating Google API client into my Rails app. I am fairly new to Rails.
I have included this in my Gemfile
gem 'google-api-client'
Then I have ran bundle in my console, in project directory
Lastly, I have created a controller, defined a route, and added
client = Google::APIClient.new(:key => "MyApiKey", :authorization => nil)
to my controller (I intend to use this with a public API key).
And when I try it in the browser, I get this:
uninitialized constant MyController::Google
Object creation without params creates same result. I have searched for a solution and tried to add require 'google/api_client' to my boot.rb, but it made no difference.
Could anyone tell me how I should approach this?
As per the official documentation you need to require the classes you are going to use, e.g.:
require 'google/api_client'
require 'google/api_client/client_secrets'
require 'google/api_client/auth/installed_app'
You should be adding this line to your controller
require 'google/api_client'
It should resolve the error.

Ruby on Rails asking to put require and enable?

I'm not sure where to put require and enable?
I'm trying to use the instagram api, but it's telling me to put
require "sinatra"
require "instagram"
enable :sessions
in the sample application : https://github.com/Instagram/instagram-ruby-gem
But I'm new to learning rails, so I'm just trying figure some bits out,
Thanks
Thats for a sintatra app, not a rails app, for rails all I think you need to do is add it to your gemfile like so.
gem 'instagram'
If you still can't use it try adding :require, though I don't think you should have to.
gem 'instagram', :require => 'instagram'

Building a new strategy for Omniauth, but Omniauth can't find it

I'm new to Rails and to Omniauth, so sorry in advance for the dumb
question.
I'm trying to do an Omniauth strategy for the Deezer website.
http://www.deezer.com/en/developers/simpleapi/oauth
First, I set up a basic rails application to test the Facebook strategy.
Here is my code in Github :
https://github.com/geoffroymontel/omniauth-test
And it works fine. Good.
Then I added those files in my app lib directory
lib/omniauth-deezer.rb
lib/omniauth/deezer.rb
lib/omniauth/deezer/version.rb
lib/omniauth/deezer/strategies/deezer.rb
and added
provider :deezer, ENV['DEEZER_APP_ID'],
ENV['DEEZER_APP_SECRET'], :perms => 'basic_access,email'
in
config/initializers/omniauth.rb
But when I start the app with
rails s
I get the following error message
/home/geoffroy/.rvm/gems/ruby-1.9.2-p290#rails3tutorial2ndEd/gems/omniauth-1.0.2/lib/omniauth/builder.rb:33:in `rescue in provider': Could not find matching strategy for :deezer. You may need to install an additional gem (such as omniauth-deezer). (LoadError) from /home/geoffroy/.rvm/gems/ruby-1.9.2-p290#rails3tutorial2ndEd/gems/omniauth-1.0.2/lib/omniauth/builder.rb:30:in `provider'
Thanks for your help
Best
Geoffroy
I added
require 'omniauth-deezer'
in initializers/omniauth.rb and it worked.
I don't really understand why I need it and Facebook doesn't.
In the Upgrading to 1.0 doc it is mentioned that Omniauth needs a gem for every provider now, so you'll have to move your provider to a gem eventually.
For future reference, if you want to add your own omniauth strategy, add this to your gemfile
gem 'omniauth-mystrategy', :path => '~/full-path-to-the-omniauth-folder/'
You´ll be using the local repository feauture of the gemfiles.
In the official bundler page you´ll see how to use remote repos also.
http://gembundler.com/v1.3/gemfile.html

Necessary steps for using a gem (e.g. json) in Ruby on Rails app?

I'm having trouble getting rubygems to work in my Rails app. Specifically, I'm trying to use the json gem, documented here: http://flori.github.com/json/ I can successfully use JSON.parse() in IRB and script/console but not in my rails app.
I know it's some combination of config.gem 'json' in environment.rb and other things, but can't find a good explanation anywhere.
Can someone give me a concise list of what is required to use this gem OR point me towards comprehensive documentation of using gems in Rails? thanks!
config.gem is used for gem dependency in rails and it does nothing more than telling rails that a gem is needed, and helping the user install the appropriate gem, etc (more details here: http://ryandaigle.com/articles/2008/4/1/what-s-new-in-edge-rails-gem-dependencies)
however by installing a gem, it should be able to be used by the rails app automatically, if not, probably you can add require "json" into environment.rb or in an .rb files in the initializers folder?
Hope it helps =)
I solved it. There's decent documentation here: http://apidock.com/rails/Rails/Configuration/gem
Once you have used config.gem in environment.rb, you should not need to 'require' it later.
My problem was that I had not stopped and restarted the server! It worked in script/console because everything was getting reloaded every time.

Resources