i have followed tutorial , working correctly.
[updated after below answer] have moved code application controller (previously defined helper) determine if current user logged in
class applicationcontroller < actioncontroller::base # prevent csrf attacks raising exception. # apis, may want use :null_session instead. protect_from_forgery with: :exception helper_method :current_user def current_user @current_user ||= user.find(session[:user_id]) if session[:user_id] end end
i have created module create client object , make api call, functionality may used 1 or more objects seemed idea create module instead of controller.
require 'base64' require 'rubygems' require 'json' require 'google/api_client' require 'google/api_client/client_secrets' require 'net/https' require 'uri' module googleclient include applicationcontroller plus_login_scope = 'https://www.googleapis.com/auth/plus.login' # build global client $credentials = google::apiclient::clientsecrets.load("#{rails.root}/config/client_secret_xxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com.json") $authorization = signet::oauth2::client.new( :authorization_uri => $credentials.authorization_uri, :token_credential_uri => $credentials.token_credential_uri, :client_id => $credentials.client_id, :client_secret => $credentials.client_secret, :redirect_uri => $credentials.redirect_uris.first, :scope => plus_login_scope) $client = google::apiclient.new(options = {:application_name => 'xxx-xxx-xxx'} ) def googleclient.get_people if current_user # authorize client , construct google+ service. $client.authorization.update_token!(current_user.oauth_token.to_hash) plus = $client.discovered_api('plus', 'v1') # list of people json , return it. response = $client.execute!(plus.people.list, :collection => 'visible', :userid => 'me').body content_type :json puts response else redirect_to root_url end end end
the user model is;
require 'google_client' class user < activerecord::base include googleclient # after_save googleclient.connect def self.from_omniauth(auth) where(provider: auth["provider"], uid: auth["uid"]).first_or_initialize.tap |user| user.provider = auth.provider user.uid = auth.uid user.name = auth.info.name user.email = auth.info.email user.oauth_token = auth.credentials.token user.oauth_expires_at = time.at(auth.credentials.expires_at) user.save! end
testing in console results in
2.1.0 :001 > googleclient.get_people nomethoderror: undefined method `helper_method' applicationhelper:module
is possible call helper method in module? how should implement code if module incorrect
** update correct module code api request has redirect uri error ** explained here in post
"notice modules in /lib not automatically loaded. instead, need add line in config/application.rb file file config block :"
config.autoload_paths += %w(#{config.root}/lib)
user model
class user < activerecord::base include google::googleclient def self.from_omniauth(auth) where(provider: auth["provider"], uid: auth["uid"]).first_or_initialize.tap |user| user.provider = auth.provider user.uid = auth.uid user.name = auth.info.name user.email = auth.info.email user.oauth_token = auth.credentials.token user.oauth_expires_at = time.at(auth.credentials.expires_at) user.save! end end if current_user self.get_people() else redirect_to root_url end end
'lib/google/google_client.rb'
require 'google/api_client' require 'google/api_client/client_secrets' require 'google/api_client/auth/installed_app' module google module googleclient # initialize client. client = google::apiclient.new( :application_name => 'xxx-xxx', :application_version => '1.0.0' ) # initialize google+ api. note make request # discovery service every time, sure use serialization # in production code. check samples more details. # load client secrets client_secrets.json. client_secrets = google::apiclient::clientsecrets.load("#{rails.root}/config/client_secret_xxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com.json") # run installed application flow. check samples more # complete example saves credentials between runs. flow = google::apiclient::installedappflow.new( :client_id => client_secrets.client_id, :client_secret => client_secrets.client_secret, :scope => ['https://www.googleapis.com/auth/plus.me'] ) client.authorization = flow.authorize def get_people # make api call. result = client.execute( :api_method => plus.activities.list, :parameters => {'collection' => 'public', 'userid' => 'me'} ) puts result.data end end end
move both of these applicationcontroller:
def current_user @current_user ||= user.find(session[:user_id]) if session[:user_id] end helper_method :current_user
that give current_user
method works in controllers, helpers, , views.