1

这是在omniauth.rb 中编写的一个初始化程序。

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2,ID,SECRET,
  {
  :approval_prompt => '',
  :scope => 'http://gdata.youtube.com,https://www.googleapis.com/auth/userinfo.email,https://www.googleapis.com/auth/userinfo.profile'
  }

返回的身份验证哈希在“信息”中没有 IMAGE 元素,为什么?????????

在下面的 HASH 中用 x 替换了原始信息

*********************
--- !ruby/hash:OmniAuth::AuthHash
provider: google_oauth2
uid: 'xxxxxxxxxxxxxxxxxxxx' 
info: !ruby/hash:OmniAuth::AuthHash::InfoHash
  name: xxxx xxx 
  email: xxxxxxxxxx 
  first_name: xxxxxx
  last_name: xxxxxxx 
credentials: !ruby/hash:Hashie::Mash
  token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 
  expires_at: 1365434778
  expires: true
extra: !ruby/hash:Hashie::Mash
  raw_info: !ruby/hash:Hashie::Mash
    id: 'xxxxxxxxxxxxxxxxxxxxxxxxxxx' 
    email: xxxxxxxxxx.xxx@gmail.com 
    verified_email: true
    name: xxxx xxxx 
    given_name: xxx 
    family_name: xxx 
    link: https://plus.google.com/xxxxxxxxxxx 
    gender: male
    locale: en
*********************

我想要个人资料图片,我做错了什么?

也试过,

{:scope => 'userinfo.email,userinfo.profile'}

不工作!

4

1 回答 1

0

不确定该范围在做什么,但我刚刚解决了为自己获取谷歌信息的问题。该信息是发送到 Rails 的实际参数,还是保存的用户哈希?

如果它不是您在登录时获得的实际参数,您可以使用:

# sessions_controller.rb
raise env["omniauth.auth"].to_yaml

这将向您展示谷歌发送给您的所有内容。

如果是用户并且如果您像我一样关注Railscasts,请检查您是否已创建一个字段来保存图片,并且您已设置该字段以保存哈希中的信息。我这样做了:

# sessions_controller.rb
def create
  user = User.from_omniauth(env["omniauth.auth"])
  user.image = env["omniauth.auth"]['info']['image']
  user.save
  session[:user_id] = user.id
  redirect_to root_url, :notice => "Signed in!"
end

如果这不是你需要的,你能更具体吗?

于 2013-05-10T14:44:47.253 回答