保存来自YouTube的OAuth登录用户的性别和位置 [英] Save user's gender and location from youtube login OAuth

查看:251
本文介绍了保存来自YouTube的OAuth登录用户的性别和位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的YouTube登录设置的OAuth2带并在一个成功的用户注册,我想保存在数据库中的用户的性别和位置。

I have youtube login setup with OAuth2 and upon a successful user registration I would like to save the users gender and location in the database.

检查OAuth的文档 https://developers.google.com/accounts/docs/ OAuth2Login#userinfocall
 你可以看到,您可以用之类的东西的姓名,电子邮件和他们的个人资料图片一起访问用户的性别和位置。

Checking the OAuth docs https://developers.google.com/accounts/docs/OAuth2Login#userinfocall you can see that you can access a users gender and location along with things like name, email and their profile picture.

下面是我的code用于保存用户信息到数据库

Below is my code for saving a users info to the db

user.rb

def self.from_omniauth(auth)
    where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.name = auth.info.name
      user.email = auth.info.email
      user.picture = auth.info.image
      user.gender = auth.info.gender
      user.country = auth.info.locale
      user.oauth_token = auth.credentials.token
      user.oauth_expires_at = Time.at(auth.credentials.expires_at)
      user.save! 
      end
     end

但使用auth.info.USER_INFO配置似乎并没有将这些值保存到数据库中。我这么想吗?

but using the auth.info.USER_INFO config doesn't seem to save these values to the database. Am i missing something?

下面是我的应用程序要求没有任何范围的参数。

Here are the parameters my app is asking for without any scopes.

更新

我得到这个由一个额外的范围内传递的参数userinfo.profile工作。请记住,包括完整的URL没有用空格分隔逗号。

I got this to work by passing in an extra scope for the userinfo.profile parameter. Remember to include full urls with no commas separated by a space.

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :youtube, YOUTUBE_KEY, YOUTUBE_SECRET, { :scope => "https://www.googleapis.com/auth/userinfo.profile https://gdata.youtube.com", access_type: 'offline', approval_prompt: '' }
end

这得到了以下权限

This got the following permissions

推荐答案

可以张贴code其中你问OAuth范围?我的猜测是,你是不是要求的轮廓范围。看看这个网站:

Can you post the code where you ask for OAuth scopes? My guess is that you aren't asking for the profile scope. Check out this site:

https://developers.google.com/oauthplayground/

如果你只是使用YouTube样本code,你很可能不会要求轮廓范围:

If you're just using the YouTube sample code, you're probably not asking for the profile scope:

https://www.googleapis.com/auth/userinfo.profile

您可以告诉如果你问这个范围或不是当你打的权威性网页,因为身份验证页面会这样说:

You can tell if you are asking for this scope or not when you hit the auth page, because the auth page will say something like:

谷歌的OAuth 2.0游乐场请求权限:
查看有关帐户的基本信息
查看你的姓名,公开个人资料网址和照片
查看您的性别和出生日期
查看你的国家,语言和时区

"Google OAuth 2.0 Playground is requesting permission to: View basic information about your account View your name, public profile URL, and photo View your gender and birthdate View your country, language, and timezone"

我已经附上截图。如果你没有看到这是,看你在哪里配置范围和它添加到OAuth范围列表授权。

I've attached a screenshot. If you're not seeing this, look to where you are configuring your scopes and add this to the list of OAUth scopes to authorize.

这篇关于保存来自YouTube的OAuth登录用户的性别和位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆