使用 OAuth 从 Twitter 获取名字/姓氏/电子邮件 [英] Getting First Name/Last Name/Email from Twitter using OAuth

查看:99
本文介绍了使用 OAuth 从 Twitter 获取名字/姓氏/电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我专门使用 omniauth 来允许通过 facebook/google/twitter 登录我的网站.

I'm using omniauth exclusively to allow login to my website with facebook/google/twitter.

我存储名字、姓氏和电子邮件.但是,当我从 oauth 提高 twitter auth 哈希时,我只能在 auth 哈希中获得昵称、名称、位置、图像、描述和网址.

I store first name, last name, and email. However, when I raise the twitter auth hash from oauth I only get nickname, name, location, image, description and urls in the auth hash.

是否可以在我的初始值设定项中传递范围以获取用户的电子邮件并将姓名分解为 first_name、last_name 字段?

Is there a scope I can pass in my initializer to get the user's email and break name out into the first_name, last_name fields?

推荐答案

Twitter 不会发送用户电子邮件,因此您将无法从 Twitter API 获取该信息.相反,您必须要求用户在您的注册表单上输入他们的电子邮件地址.

Twitter does not give out user emails so you will not be able to get that information from the Twitter API. Instead, you have to ask the user to type in their email address on your sign up form.

就拆分名称而言,一旦您使用以下内容返回哈希,您就可以这样做:

As far as splitting the name up, you'd do that once you have the hash returned using something like:

social_params ||= request.env["omniauth.auth"]
fullname = social_params["user_info"]["name"].split(' ')
first_name, last_name = fullname[0], fullname[1]
puts "first name is #{first_name} and last name is #{last_name}"

只要记住 last_name 可能是 nil,如果他们的名字中没有空格或者他们没有提供姓氏.这也没有考虑到许多人在其他文化中拥有多个姓氏这一事实.

Just keep in mind that last_name could be nil if they don't have a space in their name or they didn't give a last name. This also doesn't consider the fact that many people have multiple last names in other cultures.

这篇关于使用 OAuth 从 Twitter 获取名字/姓氏/电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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