I'm trying to get GitHub users' location using Omniauth. Everything works fine except the location (I get nil even if mine is set to "Paris"). The documentation says it should be "info.location" to retrieve it. Here is my code in my Rails app:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.nickname = auth.info.nickname
user.name = auth.info.name
user.location = auth.info.location
end
end
end
Is it possible to retrieve GitHub Users' location?
Related
I'm using oauth2 with devise and trying to login with instagram. But, when I login, it gives me this error:
You must include a valid client_id, response_type, and redirect_uri parameters
I have placed the client id in my application.yml file but its not working
instagram.rb
Instagram.configure do |config|
config.client_id = ENV['CLIENT_ID']
config.client_secret = ENV['CLIENT_SECRET']
end
callbacks_controller.rb
class CallbacksController < Devise::OmniauthCallbacksController
def instagram
#user = User.from_omniauth(request.env["omniauth.auth"])
sign_in_and_redirect #user
end
end
application.yml
CLIENT_ID : "221ea35dc7c1489c946e2f3062e8984a"
CLIENT_SECRET: "e8a100e6f1d44842a26f6bf7c4c5a063"
REDIRECT_URI : "http://localhost:3000/users/auth/instagram/callback"
routes.rb
Rails.application.routes.draw do devise_for :users, :controllers => { :omniauth_callbacks => "callbacks" }
root 'products#index'
end
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:instagram]
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.password = Devise.friendly_token[0,20]
end
end
...
end
I am trying to implement reddit login in rails application using omniauth-reddit
login is succesfull but i don't no where its going wrong
My OmniauthCallbacksController.rb file
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def reddit_oauth2
#user = User.from_omniauth(request.env["omniauth.auth"])
sign_in_and_redirect root_path
end
My User Model
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
devise :omniauthable, :omniauth_providers => [:reddit]
def self.from_omniauth(auth)
where(provider: auth.provider,uid: auth.id).first_or_create do |user|
user.provider = auth.provider
user.id = auth.id
user.password = Devise.friendly_token[0,20]
end
end
end
can anybody suggest me scope or solution ?? Thanks in Advance
After spending 30+ hours trying to get a solution to this and searching all corners of the earth, I am posting my first question here ever.
No matter what I try, I get
'The parameter app_id is required'
I've followed the docs to a T from https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview
Here are the pieces I thought were most important...any ideas what I could be missing?
config/initializers/devise.rb
config.omniauth :facebook, ENV["FACEBOOK_APP_ID"], ENV["FACEBOOK_APP_SECRET"], scope: 'user'
I've checked that the env params are correct on my system.
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable, omniauth_providers: [:facebook]
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
user.email = data["email"] if user.email.blank?
end
end
end
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
# user.name = auth.info.name # assuming the user model has a name
# user.image = auth.info.image # assuming the user model has an image
end
end
end
Try this instead :
config.omniauth :facebook, "xxxxx", "xxxx"
Using the actual values inside config/initializers/devise.rb worked for me.
Pulling my hair out on this : being redirected to user/sign_up - here is my code:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable, :omniauthable, :omniauth_providers => [:facebook]
def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
where(auth.slice(:provider, :uid)).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.name = auth.info.name # assuming the user model has a name
## user.image = auth.info.image # assuming the user model has an image
end
end
end
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
omniauth = request.env["omniauth.auth"]
#user ||= User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)
Rails.logger.level = 0
logger.debug "Session: #{#session.inspect}"
logger.debug "USer: #{#user.inspect}"
logger.debug "Omniauth: #{#omniauth.inspect}"
if #user.persisted?
sign_in_and_redirect #user, :event => :authentication #this will throw if #user is not activated
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
end
And here is the relevant logger output:
Session: nil
Processing by Devise::RegistrationsController#new as HTML
USer: #
Omniauth: nil
Redirected to http://secret-brushlands-1375.herokuapp.com/users/sign_up
Not sure what to make of this - the Facebook link is fine but my guess is the omniauth hash should not be nil and same of the session... ANy help appreciated.
I am trying to integrate omniauth-facebook into my web site but I keep getting the following err (or something similar) on callback:
NoMethodError (undefined method name=' for #<User:0x007f507c6f9158>):
app/models/user.rb:14:inblock in find_for_facebook_oauth'
app/models/user.rb:9:in find_for_facebook_oauth'
app/controllers/users/omniauth_callbacks_controller.rb:4:infacebook'
Here is my user.rb:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable, :omniauthable, :omniauth_providers => [:facebook]
def self.find_for_facebook_oauth(auth)
where(auth.slice(:provider, :uid)).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.name = auth.info.name
user.image = auth.info.image
end
end
end
Not sure why it is complaining about procedure "name" - shouldn't it be looking for a field ? Any help appreciated.
You are getting undefined method name=' for #<User:0x007f507c6f9158> error on
user.name = auth.info.name
It just means that you don't have a field named name in users table. So, the setter method name= (Note = at the end of method name) does not exist in the User model.
If you are planning to use name field on users then add it in the users table.
Create a migration file for adding name field in users table by following command
rails generate migration AddNameToUsers name:string
Run rake db:migrate