Facebook OAuth with Devise in Ruby on Rails - ruby-on-rails

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.

Related

how to get a valid client_id, response_type, and redirect_uri parameters instagram rails

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

GitHub Omniauth retrieve user location

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?

reddit-omniauth gem getting " you sent an invalid request — invalid scope requested"

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

NoMethodError with Devise and Omniauth -rails

I am in the process of trying to integrate Omniauth Facebook login with Devise for a Rails application that I'm currently in the early stages of developing. I'm a (very) junior developer, and I've hit a roadblock which I'm unsure how to resolve.
So far, I have managed to install the Devise gem successfully, and have managed to get the Omniauth Facebook working up to the point of logging into the app with Facebook (by following the documentation at https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview) - however, on the redirect back to my app, the following error message displays:
NoMethodError in Users::OmniauthCallbacksController#facebook
undefined method `name=' for #
user.name = auth.info.name # assuming the user model has a name
The code snippets that I think would be helpful to resolve the issue are as follows:
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,
: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
My routes file:
Rails.application.routes.draw do
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
root to: "restaurants#index"
devise_scope :user do
get 'sign_out', :to => 'devise/sessions#destroy', only: :destroy_user_session
end
resources :restaurants do
resources :reviews
end
end
I hope that this is enough to help me diagnose the problem, though please do let me know if any further code is required. Huge thanks in advance!
Does your user model have a name and image? Try removing these two lines:
user.name = auth.info.name # assuming the user model has a name
user.image = auth.info.image # assuming the user model has an image

Devise/Facebook callback NoMethodError (undefined method `name=' for #<User:0x007

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

Resources