I am using the oauth gem to authenticate users through GitHub. I'm trying to get the person's username and avatar url. I can't find, or completely missed, where in the scopes I can get access to these things. This is from my user model and my initializer file:
# omniauth.rb
scope: "user, public_repo, repo"
# user.rb
def self.create_with_omniauth(auth)
create! do |user|
user.provider = auth["provider"]
user.uid = auth["uid"]
user.name = auth["info"]["name"]
user.email = auth["info"]["email"]
end
end
I have tried guessing what might get the username and avatar url with:
user.username = auth["info"]["login"]
user.avatar = auth["info"]["avatar_url"]
This will return their username: auth.info.nickname
Also you can use this api to get avatars from the github api:
Get GitHub avatar from email or name
Related
Currently, login of facebook is going well, I can get profile picture.
However, only getting name is not an ordinary name, it is displayed as 4f427j700ef46f2555e6.
How to get it
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.provider = auth.provider
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.avatar = 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!
end
end
Display method
<h2><%= #user.name %></h2>
Why can not I get a name?
please tell me.
How can i get Facebook user email by omniauth for Rails?
This is my omniauth.rb
OmniAuth.config.logger = Rails.logger
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, ENV['FACEBOOK_KEY'] , ENV['FACEBOOK_SECRET'], :scope => 'email', :display => 'popup', :info_fields => 'name,email'
end
This is my model
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user|
puts "auth.providerauth.providerauth.providerauth.provider"
puts auth.info.inspect
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.username = auth.info.name.gsub(" ","") + new_token
user.email = auth.info.email
user.password = digest(new_token)
user.save!
end
end
But i only got this from the info
auth.providerauth.providerauth.providerauth.provider
#<OmniAuth::AuthHash::InfoHash image="http://graph.facebook.com/xxxx" name="xxx xxxx">
Seems like no email in return.
But i need the email to pass my model validation so what should i do?
Thanks!
in theory the user.auth.info.email should be the case
https://github.com/mkdynamic/omniauth-facebook
... but in reality Facebook don't necessary need to return email if user don't want to publish email. (for example Twitter will never give you response with email)
so the approach where you creating user directly from what Oauth callback returns may be wrong for you.
if you really rally need an email from user, try rather saving whatever the OAuth returns to an Identity model and then if email is present create User and if email is not present prompt the user to provide the email in "finish signup form" that will create the user.
Think about it: Technically speaking Oauth is a protocol where user can signup to your application without providing any credentials => the Provide-UID is what defines him not the email
So you either split the way how you handle session to User(email&password) or Identity(OAuth) so someting like current_user || current_identity in your controllers ....or you will link them when email is provided
It is present in auth.
auth.info.email
I am trying to use the YT gem to retrieve channel information from YouTube but I keep on getting the following error:
uninitialized constant ChannelsController::Yt
I am using Devise and Omniauth Google and when the user logs in, I am saving their access token to the database as part of my from_omniauth method as such:
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.access_key = auth['credentials']['token']
end
end
Then trying to pass this to YT::Account.new as such:
def edit
# get youtube account
#account = Yt::Account.new access_token: current_user.access_token
email = #account.email
end
YT is install in my gem file as such:
gem 'yt', '~> 0.25.5'
I am at a bit of a loss of what is going on, any thoughts?
Many thanks
David
I'm trying to create a Rails 4 app with Facebook login using devise and omniauth-facebook gem.
I followed this tutorial https://www.digitalocean.com/community/tutorials/how-to-configure-devise-and-omniauth-for-your-rails-application
Right now, I can save a user in the database, but email gets null.
I already tried another tutorials, all of them getting the same, email null.
I noticed that when I click on the "Sign in" button, I get the following link:
https://www.facebook.com/login.php?skip_api_login=1&api_key=1452548131713566&signed_next=1&next=https%3A%2F%2Fwww.facebook.com%2Fv2.4%2Fdialog%2Foauth%3Fredirect_uri%3Dhttp%253A%252F%252Flocalhost%253A3000%252Fusers%252Fauth%252Ffacebook%252Fcallback%26state%3Db9cfa351943875b105b0b9206a5de31d6fbed43cda4a59d3%26scope%3Demail%26response_type%3Dcode%26client_id%3D1452548131713566%26ret%3Dlogin&cancel_url=http%3A%2F%2Flocalhost%3A3000%2Fusers%2Fauth%2Ffacebook%2Fcallback%3Ferror%3Daccess_denied%26error_code%3D200%26error_description%3DPermissions%2Berror%26error_reason%3Duser_denied%26state%3Db9cfa351943875b105b0b9206a5de31d6fbed43cda4a59d3%23_%3D_&display=page
It seems like a permission error, but I can't seem to figure it out why.
Here it is the self.from_omniauth method:
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.name = auth.info.name
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
end
end
Could You Please Check params by using
raise params.inspect
on the callback page action as when,facebook says that app is sharing user's email.It sends the json data from its system to your callback url.
The api changed on 8 of July.
This did it:
config.omniauth :facebook, "appid", "appsecret", scope: 'email', info_fields: 'email'
https://developers.facebook.com/docs/apps/changelog#v2_4
I'm using the omniauth-linkedin gem to allow users to log into my Rails application using their LinkedIn account. I'm currently using auth.info.image to store the user's LinkedIn profile image URL:
user.rb
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.first_name = auth.info.first_name
user.last_name = auth.info.last_name
user.email = auth.info.email
user.linkedin_photo_url = auth.info.image
user.password = Devise.friendly_token[0,20]
end
However, the image is very small (50x50). Is there another method besides auth.info.image I could use in order to pull the large profile image found on the user's main profile page?
Thanks!
EDIT: I'm using the omniauth-linkedin and omniauth gems. It looks like the linkedin gem has a method with an option to determine image size but I'm struggling with implementing it with the omniauth-linkedin gem. This readme explains that it's possible but the explanation is lacking some details. Can someone help me figure this out?
https://github.com/skorks/omniauth-linkedin#using-it-with-the-linkedin-gem
I know it's been awhile, but I was just looking for this and thought I'd leave it here. The solution is fine, but will cause an extra call. Omniauth is already doing a fetch of the profile so we just have to tell it to also get the original picture
linkedin_options = {
scope: 'r_fullprofile r_emailaddress',
fields: ['id', 'email-address', 'first-name', 'last-name', 'headline', 'location', 'industry', 'picture-url', 'public-profile-url', "picture-urls::(original)"]
}
provider :linkedin, app_id,app_secret, linkedin_options
pictureUrls will be available in the extra info.
To get the image, use auth_hash[:extra][:raw_info][:pictureUrls][:values].first
One way to retrieve profile image in original size is by making separate API call.
include gem 'linkedin'
create initializer file /config/initializers/linkedin.rb with content:
LinkedIn.configure do |config|
config.token = "your LinkedIn app consumer_key"
config.secret = "your consumer_secret"
end
in your self.from_omniauth method replace line
user.linkedin_photo_url = auth.info.image
with
client = LinkedIn::Client.new
client.authorize_from_access(auth.extra.access_token.token, auth.extra.access_token.secret)
user.linkedin_photo_url = client.picture_urls.all.first
DONE
image = auth.extra.raw_info.pictureUrls.values.last.first
This is using a combination of the omniauth gem, devise, and paperclip that works for me:
config/initializers/devise.rb
config.omniauth :linkedin, ENV['LINKEDIN_KEY'], ENV['LINKEDIN_SECRET'],
scope: 'r_basicprofile r_emailaddress',
fields: ['id', 'email-address', 'first-name', 'last-name', 'picture-urls::(original)']
app/models/user.rb
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create.tap do |user| # .tap will run the |user| block regardless if is first or create
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.firstname = auth.info.first_name
user.lastname = auth.info.last_name
if auth.provider == 'facebook'
user.avatar = URI.parse(auth.info.image)
elsif auth.provider == 'linkedin'
user.avatar = URI.parse(auth.extra.raw_info.pictureUrls.values.last.first)
end
user.skip_confirmation!
end
end