Retrieving a Google Profile Image Using Omniauth on Rails 3

I am launching a new Rails 3 application using Omniauth for authentication through Facebook, Twitter and Google. I can easily get a user avatar from Facebook and Twitter, but I can’t find a way to get it from Google, if it exists.

Here is the code I use to create the authentication hash:

omniauth['user_info']['email'] ? @authhash[:email] = omniauth['user_info']['email'] : @authhash[:email] = '' omniauth['user_info']['name'] ? @authhash[:name] = omniauth['user_info']['name'] : @authhash[:name] = '' omniauth['uid'] ? @authhash[:uid] = omniauth['uid'].to_s : @authhash[:uid] = '' omniauth['provider'] ? @authhash[:provider] = omniauth['provider'] : @authhash[:provider] = '' 

On Twitter and Facebook, this next line gets an avatar or sets a default value if not specified:

 omniauth['user_info']['image'] ? @authhash[:image] = omniauth['user_info']['image'] : @authhash[:image] = 'avatar.jpg' 

This does not work on Google and I could not find the documentation.

Any ideas?

Thanks a lot!

+4
source share
3 answers

Try the "Omniauth Google OAuth2 Strategy", in which the commit is committed:

Returns first name, first name, last name, first name, email address in the information: scope => 'userinfo.email, userinfo.profile'

You can view the commit here

+7
source

Yes, you can get a picture, although I would think that it depends on which versions you use.

im using

rvm current ruby-1.9.3-P194

 > gem list oauth (0.4.6) oauth2 (0.8.0) omniauth (1.1.0, 1.0.3) omniauth-facebook (1.4.1, 1.4.0) omniauth-google (1.0.1) omniauth-google-oauth2 (0.1.13) omniauth-oauth (1.0.1) omniauth-oauth2 (1.1.0, 1.0.3) omniauth-twitter (0.0.12) 

I came here for another reason, because the naming convention for accessing profile properties is different from the explanations in various textbooks, as I experienced an error when completing the sign. In fact, in your question, you may find that you will have the same problems.

The problem is that google has different property names from FB, Twitter, etc., so you need to keep this in mind.

To find the properties, I commented out the executable bit and simply discarded the answer. So.

 elsif service_route == 'google_oauth2' render :text => omniauth.to_yaml return 

Your google profile information will be displayed here, which we hope is as follows.

 --- !ruby/hash:OmniAuth::AuthHash provider: google_oauth2 uid: '1234567' info: !ruby/hash:OmniAuth::AuthHash::InfoHash name: Your Name email: yourEmail first_name: firstname last_name: surname image: https://animage credentials: !ruby/hash:Hashie::Mash token: aToken expires_at: 123 expires: true extra: !ruby/hash:Hashie::Mash raw_info: !ruby/hash:Hashie::Mash id: '123456' email: YourEmail verified_email: true name: YourName given_name: Name family_name: surname link: https://plus.google.com/blah picture: https://lh6.googleusercontent.com/blah blah gender: male birthday: '' locale: en-GB 

As you can see, the parameter names are different, get rid of user_info and have information instead.

You will also notice that you have an image: and an image: so until I tried, I would assume that this is your profile.

 elsif service_route == 'google_oauth2' omniauth['info']['email'] ? email = omniauth['info']['email'] : email = '' omniauth['info']['name'] ? name = omniauth['info']['name'] : name = '' omniauth['uid'] ? uid = omniauth['uid'] : uid = '' omniauth['provider'] ? provider = omniauth['provider'] : provider = '' omniauth['info']['image'] ? image = omniauth['info']['image'] : image = '' 
+2
source

I had the same problem, and it turned out that the profile image was not publicly available. I had to change the setting "Available only to people with whom I can communicate" with "Visible to all." Then the image appeared. Hope this helps someone.

0
source

All Articles