我需要从Facebook获取姓名,电子邮件,图片和性别.我得到了名字和图片,但是没有从Facebook获取电子邮件和性别.我过去2天都在挣扎,任何人都可以帮助我.
用户模型:
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.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.gender = auth.extra.raw_info.gender #if user.gender.blank?
user.image = auth.info.image
user.save!
end
end
Run Code Online (Sandbox Code Playgroud)
Omniauth.rb
OmniAuth.config.logger = Rails.logger
Rails.application.config.middleware.use OmniAuth::Builder do
if Rails.env.production?
provider :facebook, 'APP_ID', 'APP_SEC'
elsif Rails.env.development?
provider :facebook, 'APP_ID', 'APP_SEC', {:scope => 'publish_actions,email', :client_options => { :ssl => { :ca_file => "#{Rails.root}/config/ca-bundle.crt" }}}
else
provider :facebook, 'APP_ID', 'APP_SEC'
end
end
Run Code Online (Sandbox Code Playgroud)
如果我使用user.gender = auth.extra.raw.gender if user.gender.blank?它返回null.
即使我已经使用我的Facebook隐私设置进行了检查,它也仅限于公开个人资料.有人可以帮帮我吗
ruby-on-rails facebook-graph-api ruby-on-rails-3 ruby-on-rails-4 omniauth-facebook
我正在使用devise/omniauth.通过facebook/twitter注册后,我想重定向到一个名为"verify-email"的页面,在那里他们可以验证他们的电子邮件地址是否正确.
我只是想让localhost:3000/users/verify-email页面立即正常工作.我去那个网址,我收到此错误消息:
找不到路径"/ users/update_email"的设计映射.这可能有两个原因:1)您忘记将路径包裹在范围块内.例如:devise_scope:user do get"/ some/route"=>"some_devise_controller"end 2)您正在测试绕过路由器的Devise控制器.如果是这样,您可以明确告诉Devise使用哪个映射:@ request.env ["devise.mapping"] = Devise.mappings [:user].
所以这是我的routes.rb:
Rails.application.routes.draw do
...
devise_for :users, :controllers => {
omniauth_callbacks: 'custom_devise_controllers/omniauth_callbacks',
registrations: 'custom_devise_controllers/registrations' }
devise_scope :users do
get "users/verify_email" => 'custom_devise_controllers/registrations#verify_email'
end
...
end
Run Code Online (Sandbox Code Playgroud)
我不认为我错误地在范围块中包装路由(这是错误消息的#1所述).这是否意味着我需要明确告诉Devise使用哪个映射(这是错误消息的#2)?错误消息说我可以告诉设计使用哪个映射:@request.env["devise.mapping"] = Devise.mappings[:user].我应该将它放在我的custom_devise_controllers/registrations_controller.rb中吗?或者你们认为还有其他事情发生了吗?
我将包含我的"custom_devise_controllers/registrations_controller.rb":
class CustomDeviseControllers::RegistrationsController < Devise::RegistrationsController
def update
@user = User.find(current_user.id)
successfully_updated = if needs_password?(@user, params)
@user.update_with_password(devise_parameter_sanitizer.sanitize(:account_update))
else
params[:user].delete(:current_password)
@user.update_without_password(devise_parameter_sanitizer.sanitize(:account_update))
end
if successfully_updated
set_flash_message :notice, :updated
sign_in @user, :bypass => true
redirect_to after_update_path_for(@user)
else
render "edit"
end …Run Code Online (Sandbox Code Playgroud) 我有一个站点,配置为使用Devise与Omniauth一起使用多个Oauth2 API,并且一直运行到上周.目前登录Twitter和Github仍然正常运行; 然而,Facebook,LinkedIn和谷歌给我一个错误,指出重定向URI不匹配.错误消息读取:
Facebook的:
错误 - omniauth:(facebook)身份验证失败!invalid_credentials:> OAuth2 ::错误,:{"error":{"message":"验证验证码时出错.请确保您的> redirect_uri与您在OAuth对话框请求中使用的相同","输入":" OAuthException", "代码":100, "fbtrace_id": "XXXXXXXXXX"}}
LinkedIn:
错误 - omniauth:(linkedin)身份验证失败!invalid_credentials:> OAuth2 :: Error,invalid_request:缺少必需参数,包含无效参数值,参数多次.:无法检索访问令牌:appId或重定向uri与授权代码或授权代码过期不匹配{"error_description":"缺少必需参数,包含无效参数值,参数多次.:无法检索访问令牌:appId或重定向uri与授权代码或授权代码过期不匹配","错误":"invalid_request"}
谷歌
错误 - omniauth:(google_oauth2)身份验证失败!invalid_credentials:> OAuth2 :: Error,redirect_uri_mismatch:{"error":"redirect_uri_mismatch"}
我查看了Chrome开发者控制台中针对所有这三个请求发送的请求,并且回调的重定向uri与每个API注册的uri相匹配(自从它工作以来没有更改).
回溯跟踪此错误的挑战是,当我安装新功能时,由于我在最近的集成测试期间直接登录或使用Github登录,因此我不能100%确定这些错误.(大课程学到了!)可能影响这一点的重大变化之一就是我整合了Devise的Traceable扩展,我需要Warden Gem.但是,我删除了Traceable和Warden配置,并将用户模型和配置文件恢复到以前的状态,我遇到了同样的问题.
我通常更愿意提供更多的代码示例,但说实话,我不确定从哪个代码开始.我希望有人遇到类似的问题,并指出正确的方向开始.
要开始,下面是我的Devise初始化程序,注释已删除以缩短
Devise.setup do |config|
config.mailer_sender = 'no-reply@' + ENV['DOMAIN_NAME']
config.mailer = 'Devise::Mailer'
require 'devise/orm/active_record'
config.case_insensitive_keys = [:email]
config.strip_whitespace_keys = [:email]
config.skip_session_storage = [:http_auth]
config.stretches = Rails.env.test? ? 1 : 10
config.allow_unconfirmed_access_for = 10.days
config.reconfirmable = true
config.confirmation_keys = [:email]
config.remember_for = 2.weeks
config.expire_all_remember_me_on_sign_out = true
config.password_length = 8..72 …Run Code Online (Sandbox Code Playgroud) ruby-on-rails devise omniauth-facebook omniauth-linkedin google-oauth2
我正在研究来自http://www.theodinproject.com/ruby-on-rails/final-project的facebook克隆项目.
我被omniauth-facebook部分困住了,我无法成功登录facebook.我认为问题可能是由于request.env ["omniauth.auth"].当我尝试提出request.env ["omniauth.auth"].to_yaml时.我得到以下不完整的哈希.它缺少很多信息,例如first_name,last_name,gender等.
--- !ruby/hash:OmniAuth::AuthHash
provider: facebook
uid: '10206926404981253'
info: !ruby/hash:OmniAuth::AuthHash::InfoHash
name: Thomas Pan
image: http://graph.facebook.com/10206926404981253/picture
credentials: !ruby/hash:OmniAuth::AuthHash
token: <token>
expires_at: 1442277104
expires: true
extra: !ruby/hash:OmniAuth::AuthHash
raw_info: !ruby/hash:OmniAuth::AuthHash
name: Thomas Pan
id: <id>
Run Code Online (Sandbox Code Playgroud)
**用<>替换了一些信息以确保安全性.
我也在使用它和设计.
其他所有东西似乎都设置正确,因为我按照这里的说明进行设计和omniauth-facebook.https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview
user.rb
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable, :omniauth_providers => [:facebook]
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.first_name = auth.info.first_name
user.last_name = auth.info.last_name
user.gender = auth.extra.raw.gender
end
end
Run Code Online (Sandbox Code Playgroud)
devise.rb
config.omniauth :facebook, …Run Code Online (Sandbox Code Playgroud)