I ve installed the gem koala and omniauth.
In my user model
class User < ActiveRecord::Base
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.image = auth.info.image
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.save!
end
end
def facebook
#facebook ||= Koala::Facebook::API.new(oauth_token)
end
end
Ive the permission from the facebook but still i am not being able to get the relationship status?
Try this
#facebook.get_object('me',:fields=>"name,gender,relationship_status")
Related
Since two days i tried to make Login with Facebook on ruby on rails using devise but it returns me NULL as email's value. I read many articles on stackoverflow but a lot of them are from years ago and none have worked for me. Thanks you for helping.
Here is the message in my console when i choose my facebook account
def self.from_omniauth(auth)
unless auth.info.email
auth.info.email = "#{auth.info.name.split(' ').join}#facebook.com"
end
user = User.where("email= ?", auth.info.email).first
puts auth.info.picture
puts auth.info.name
puts auth.info.email
if user
return user
else
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
# If you are using confirmable and the provider(s) you use validate emails,
# uncomment the line below to skip the confirmation emails.
user.skip_confirmation!
user.uid = user.uid
user.provider = user.provider
end
end
I'm trying to list all the emails from inbox in a ruby on rails app I have the login and authentication working with omniauth-google-oauth2 now I want to list all the inbox messages so in my controller:
home_controller.rb I have this:
class HomeController < ApplicationController
def index
if session[:user_id]
client = Signet::OAuth2::Client.new(access_token: session[:access_token])
client.expires_in = Time.now + 1_000_000
service = Google::Apis::GmailV1::GmailService.new
service.authorization = client
#labels_list = service.list_user_labels('me')
else
puts "ERROR !!!"
end
end
end
and in my User model I have this:
class User < ApplicationRecord
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.save!
end
end
end
I have this error:
Missing token endpoint URI.
I dont know how to resolve this I tried everything in stackoverflow and documentation.
Regards and thanks for your time guys !
I'm using the omniauth-facebook gem in combination with Devise to enable users to login with their Facebook account. This works fine. The problem now lies in the creation of a new user, when first signing up with Facebook. Each User has one ExtendedProfile with detailed user info. So the email and password are columns in the users table, but location and profile_image are stored in the extended_profiles table.
When signing up for the first time, the following method is executed:
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]
end
end
Now this creates a record for the users table, but not an extended_profile. Any idea how I can create a record for extended_profiles with the right user_id and fill it with the information I get back from Facebook? Thanks very much in advance!
So I've found the solution while trying out house9's answer:
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.create_extended_profile(
:first_name => auth.info.first_name,
:last_name => auth.info.last_name,
:facebook_avatar => auth.info.image)
end
end
The user.create_extended_profile() line creates a relational record with the auth values filled in! :)
Have you tried setting it in the block? Not sure if this will work
def self.find_for_facebook_oauth(auth)
where(auth.slice(:provider, :uid)).first_or_create do |user|
user.provider = auth.provider
# ....
user.extended_profile.build
end
end
another option is to put an after_create callback on the User to create the record
http://guides.rubyonrails.org/active_record_callbacks.html#available-callbacks
or you can conditionally check if the record exists, change up that code to:
def self.find_for_facebook_oauth(auth)
user = where(auth.slice(:provider, :uid)).first
unless user
user = User.new
user.provider = auth.provider
# ....
user.extended_profile.build
user.save
end
user
end
I am using devise with omniauth facebook. I upload avatars for people that are not signing in with facebook with carrierwave, with the AvatarUploader.
But when I try fetching the avatar from the facebook user, it does not save - user.avatar = auth.info.image does not save, and after signing in equals nil.
Why is that and how can I fix it? Thanks in advance.
def self.from_omniauth(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.avatar = auth.info.image
end
end
Have you tried this?
In your method, change the line
user.avatar = auth.info.image
to
user.remote_avatar_url = auth.info.image
I am using Omniauth-facebook gem from Github from Ruby on Rails. I have several questions regarding the omniauth-facebook gem.
I am using the following function get the data from Facebook, but this function is called every time the user signs in and in registration.
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
user.provider = auth.provider
user.id = auth.uid
user.uid = auth.uid
user.name = auth.info.name
user.username = auth.info.nickname
user.email = auth.info.email
user.gender = auth.extra.raw_info.gender
user.is_admin = false
user.picture = "http://graph.facebook.com/#{auth.uid}/picture?type=large&height=324&width=580"
user.updated_at = auth.extra.raw_info.updated_time
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.save!
end
end
How can I differ the registration and login function from each other?
On the other hand, I want to post the user's wall something when the user signs in, or posts something. How can I do it in Omniauth-facebook gem?
Thank you.
Omniauth-facebook gem is best if want to add any post on users wall u can refer this link
facebook share