开发者

Retrieving Google profile picture with Omniauth on Rails 3

I'm starting a new Rails 3 app using Omnia开发者_开发知识库uth for authentication via Facebook, Twitter and Google. I can easily get the user's avatar from Facebook and Twitter, but can't find a way to retrieve it from Google, if existent.

Here's the code I use to build 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 the avatar or sets to the default if not provided:

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

This doesn't work on Google and I couldn't find any documentation on that.

Any ideas?

Thanks a lot!


Try 'Omniauth Google OAuth2 Strategy' in which a commit states:

Returns name, first_name, last_name, image, email in info with :scope => 'userinfo.email,userinfo.profile'

You can view the commit here


Yes you can get the picture, though i would imagine it depends on what versions your using.

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 arrived here for a different reason that was due to the fact that the naming convention to access the profile properties was different from those explain in various tutorials, as such i was experiencing error completing the sign in. In fact in your question you may find that you will have the same issues.

The problem is that google has different property names from FB, Twitter, etc. so you have to account for that.

To find the properties i commented out the doing bit, and just dumped the response out. like so.

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

This will output your google profile details which will hopefully look like the following.

--- !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 names of the parameters are different, get rid of user_info and instead have info.

You will also notice that you have picture: and image: so whilst i have not tried this i would assume that it is your profile picture.

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 = ''


I was having the same issue and found out that it was just that the image of the profile wasn't public. I had to change the settings from 'Visible only to people I can chat with' to 'Visible to everyone'. Then the image started to show up. Hope this helps someone.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜