新手问题 新浪微博授权不提供 email 怎么办?

yuchengzhixia · 2012年12月05日 · 最后由 aoner 回复于 2014年05月19日 · 6611 次阅读

在用 devise+omniauth-weibo-oauth2 写一个三方帐号登录,但是新浪微博不提供 email 的,而 email 为登录必须字段,每次用微博登录后又回到登录界面,我的想法是将授权信息存入 session,然后跳转到让用户补充 email 的界面,必须让用户绑定一个 email 才能完成授权,但是不知道具体该怎么去写。

参照https://github.com/plataformatec/devise/wiki/OmniAuth%3A-Overview写的方法如下: def self.find_for_weibo_oauth2(auth, signed_in_resource=nil) user = User.where(:provider => auth.provider, :uid => auth.uid).first unless user user = User.create(username:auth.extra.raw_info.name, provider:auth.provider, uid:auth.uid, email:auth.info.email, password:Devise.friendly_token[0,20] ) end user end

def weibo # You need to implement the method below in your model (e.g. app/models/user.rb) @user = User.find_for_weibo_oauth2(request.env["omniauth.auth"], current_user) # debugger if @user.persisted? flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Weibo" sign_in_and_redirect @user, :event => :authentication else session["devise.weibo_data"] = request.env["omniauth.auth"] redirect_to new_user_registration_url end end

出个表单,让用户填写呀

还有一个办法是根据 uid 生成一个不会重复的 email 地址,先用这个 email 地址来注册用户。 比如这个 user.email = "douban+#{uid}@example.com" if provider == "douban"

但是最后还是要在用户信息页面处理这种特殊的 mail。

def new_user
  @auth_hash = MultiJson.load(params[:auth] || '{}')
  email = params[:email]
  if User.where(:email => email).exists?
    flash[:error] = 'Email 已经被注册过了'
    render :template => 'auth/weibo_login'
  else
    user = User.create_by_email_and_auth(email, @auth_hash)
    if user
      user.update_attribute(:last_signed_in_at, Time.now)
      self.current_user = user
      redirect_to root_path
    else
      flash[:error] = '注册出错了...'
      render :template => 'auth/weibo_login'
    end
  end
end
render
redirect_to

看情况使用吧,都可以实现,需要做的就是给用户一个界面,让他填写就可以了!

既然拿到 token 了就再调用一次 user_profile 的接口可以么?

#2 楼 @xmonkeycn 我不想让用户修改他的邮箱

#5 楼 @windlx user_profile 没有 email 呀

接口又变了?@@......新浪真辣手

#1 楼 @huacnlee 请教下 devise 的 sign_in_and_redirect 方法

flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "GitHub" if is_navigational_format?
      sign_in_and_redirect @user, :event => :authenticatio

sign_in_and_redirect 登录成功后好像不能跳转,重写了 after_sign_in_path_for 函数也不行。假设第三方登录按钮在网站首页,登录成功还是在首页,怎么跳转到用户主页

需要 登录 后方可回复, 如果你还没有账号请 注册新账号