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.
Related
I am creating rails API app for managing Trello related boards and lists.
I have decided to use "ruby-trello" gem.
There is a config example at github ruby-trello page (https://github.com/jeremytregunna/ruby-trello).
Config looks like this:
Trello.configure do |config|
config.developer_public_key = 'my_trello_key'
config.member_token = 'my_trello_token'
end
I got key and token from trello devs.
I have added this code to my config/application.rb in class Application.
But when I am trying to get something data from Trello I see this error:
Trello::ConfigurationError (Trello has not been configured to make authorized requests.)
Can anybody help? Where I need to place this config code?
Sorry for my English.
The Rails convention is to put that in config/initializers/trello.rb so that it's picked up when Rails boots.
It's odd they don't seem to mention that in the documentation.
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.
I need to get the username of the currently logged windows user. Could it be done easily?
The username of the account running the script can be accessed via something like:
puts ENV['USERNAME']
Beware that if you're running it as a system service the username will probably come back as "SYSTEM"
If that isn't enough to suite your needs there is an alternative method outlined here: https://stackoverflow.com/a/3544741/648695
You can use the Ruby etc module
require 'etc'
Etc.getlogin
The doc is avaiable here: http://ruby-doc.org/stdlib-2.0.0/libdoc/etc/rdoc/Etc.html#method-c-getlogin
Returns the short user name of the currently logged in user.
As far as I know, unless you're using active directory that would require a microsoft framework website, I don't think you'll find a way in rails.
There are a few discussion points here that may help: Can you get a Windows (AD) username in PHP?
Same question here: is there a way to read a clients windows login name using ruby on rails
Anyways, just copying my own answer below...
This is what worked for me but there are some limitations:
won't work in Chrome: undefined method 'encode' for nil:NilClass
won't validate user credentials
If you don't care about these issues, go ahead:
In your rails application, add Rekado's gem to your Gemfile: gem 'ntlm-sso', '=0.0.1'
Create an initialiser config/initializers/ntlm-sso.rb with:
require 'rack'
require 'rack/auth/ntlm-sso'
class NTLMAuthentication
def initialize(app)
#app = app
end
def call(env)
auth = Rack::Auth::NTLMSSO.new(#app)
return auth.call(env)
end
end
On your application.rb file, add the line: config.middleware.use "NTLMAuthentication"
Call request.env["REMOTE_USER"] on your view or controller to get current username.
PS: Let me know if you find anyway to make it work on Chrome or to validate user credentials.
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'
I am writing a plugin in Rails 3.2.6 - my plugin is supposed to modify my User model (located in app/models/user.rb) - however, I cannot find the proper way to reference that file from the plugin in a require statement.
I tried require 'user', require 'app/models/user', and a bunch of stuff trying to use Rails.root and other variables - nothing works and I get an error "Uninitialized constant User" when trying to run the app.
Can someone please point me in the right direction? BTW, this plugin was build as a gem and is included in my Gemfile (which pulls it from github).
Thanks!
If you are on ruby 1.9, you should be able to use require_relative
#assuming /lib/yourfile.rb
require_relative '../app/models/user.rb'
If you are on an older ruby, you might try
require File.dirname(__FILE__) + '../app/models/user.rb'