Large profile image with omniauth linkedin Ruby gem - ruby-on-rails

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

Related

Oauth facebook with ruby on rails 5.2.5

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

Facebook login name getting wrong successfully

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.

uninitialized constant Controller::Yt

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

Get username and avatar url through github authenticaton

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

best omniauth gem for rails

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

Resources