2

我正在拼命寻找一种方法来让设计在我的 Rails 4 应用程序中工作。我已经尝试了所有可以找到的教程来进行设置。

当前教程是:http: //sourcey.com/rails-4-omniauth-using-devise-with-twitter-facebook-and-linkedin/

我之前关于相关问题的赏金问题(通常被否决,我不明白为什么),展示了我完成这项工作的其他尝试。

我目前的问题是用户模型中的这段文字:

def self.find_for_oauth(auth, signed_in_resource = nil)

    # Get the identity and user if they exist
    identity = Identity.find_for_oauth(auth)

    # If a signed_in_resource is provided it always overrides the existing user
    # to prevent the identity being locked with accidentally created accounts.
    # Note that this may leave zombie accounts (with no associated identity) which
    # can be cleaned up at a later date.
    user = signed_in_resource ? signed_in_resource : identity.user

    # Create the user if needed
    if user.nil?

      # Get the existing user by email if the provider gives us a verified email.
      # If no verified email was provided we assign a temporary email and ask the
      # user to verify it on the next step via UsersController.finish_signup
      email_is_verified = auth.info.email && (auth.info.verified || auth.info.verified_email)
      email = auth.info.email if email_is_verified
      user = User.where(:email => email).first if email

      # Create the user if it's a new registration
      if user.nil?
        user = User.new(
          name: auth.extra.raw_info.name,
          #username: auth.info.nickname || auth.uid,
          email: email ? email : "#{auth.uid}-#{auth.provider}.com",
          password: Devise.friendly_token[0,20]
        )
        user.skip_confirmation!
        user.save!
      end
    end

问题是(指向这一行:user = User.new)在上述方法中:

unknown attribute 'name' for User.

在我尝试的上一个教程中,我发现当我将属性更改为时,linkedin 工作:

# self.email = omniauth['extra']['raw_info']['emailAddress'] 
 #      self.first_name = omniauth['extra']['raw_info']['firstName']
    #     self.last_name = omniauth['extra']['raw_info']['lastName']

omn​​iauth 策略的字段名称与教程中显示的不同(至少对于linkedin)。但是,如果 Facebook 或 twitter 再次使用不同的字段名称,本教程如何解决这个问题?

此外,在我的用户表中,我有名为 first_name 和 last_name 的属性,但我尝试将行更改为:

first_name: auth.extra.raw_info.'firstName',

那也没用。

当我尝试:

first_name: auth.extra.raw_info.name

我收到此错误:

undefined method `password_required?' for #<User:0x007fb7bc2ce6f8>

但无论如何,我只想要该字段中的名字,我认为这是将整个名字放入名字中(尽管我不确定)。此外,如果这将被修改为适用于linkedin,这是否意味着它不适用于 Facebook 和 twitter?

这都是一个大麻烦。我越来越对此感到沮丧。有谁知道如何解决这个特定问题。我已经尝试了 2.5 年来让设计/omniauth 工作。

我最近以前的教程链接问题是:

https://stackoverflow.com/questions/33888972/rails-devise-omniauth-strategies-omniauthcallbackscontroller

设计 Omniauth - 设置和定义策略

Rails、Devise 和 Omniauth - 设置问题

还有其他几个,但我并没有通过自己的努力解决这个问题。我在 codementor.io 上进行了几次会议,但未能找到帮助。帮助来源将不胜感激。

所以尝试下面的建议,我尝试将用户模型中的方法更改为:

def self.find_for_oauth(auth, signed_in_resource = nil)

    # Get the identity and user if they exist
    identity = Identity.find_for_oauth(auth)

    # If a signed_in_resource is provided it always overrides the existing user
    # to prevent the identity being locked with accidentally created accounts.
    # Note that this may leave zombie accounts (with no associated identity) which
    # can be cleaned up at a later date.
    user = signed_in_resource ? signed_in_resource : identity.user

    # Create the user if needed
    if user.nil?

      # Get the existing user by email if the provider gives us a verified email.
      # If no verified email was provided we assign a temporary email and ask the
      # user to verify it on the next step via UsersController.finish_signup
      email_is_verified = auth.info.email && (auth.info.verified || auth.info.verified_email)
      email = auth.info.email if email_is_verified
      user = User.where(:email => email).first if email

      # Create the user if it's a new registration
      if user.nil?
        user = User.new(
          case auth.provider
            when 'linkedin'
              first_name: auth.extra.raw_info.firstName,
              last_name: auth.extra.raw_info.lastName,
              email: emailAddress ? email : "#{auth.uid}-#{auth.provider}.com",
              #username: auth.info.nickname || auth.uid,
              # email: email ? email : "#{auth.uid}-#{auth.provider}.com",
              password: Devise.friendly_token[0,20]
            when 'facebook'
              first_name: auth.extra.raw_info.first_name,
              last_name: auth.extra.raw_info.last_name,
              email: auth.extra.raw_info.email ? email : "#{auth.uid}-#{auth.provider}.com",
              password: Devise.friendly_token[0,20]

            when 'twitter' 
            first_name: auth.extra.raw_info.nickname, 
          end
        )
        user.skip_confirmation!
        user.save!
      end
    end

这样做有几个问题,分别是:

unexpected ':', expecting keyword_end first_name: auth.extra.raw_info.firstName,
unexpected tLABEL, expecting '=' last_name: auth.extra.raw_info.lastName
unexpected tLABEL, expecting '=' email: emailAddress ? email : "#{au... ^
unexpected ',', expecting keyword_end
unexpected keyword_when, expecting keyword_end when 'facebook' ^
unexpected ':', expecting keyword_end first_name: auth.extra.raw_info.first_name, ^ 
unexpected tLABEL, expecting '=' last_name: auth.extra.raw_info.last_name,
unexpected tLABEL, expecting '=' email: auth.extra.raw_info.email ? ... ^
unexpected ',', expecting keyword_end
unexpected keyword_when, expecting keyword_end when 'twitter' ^ 
unexpected ':', expecting keyword_end first_name: auth.extra.raw_info.nickname, ^
unexpected keyword_end, expecting '='

我不知道如何解决任何这个问题——我找不到任何关于如何解决这个问题的例子(除了下面帖子中的例子)——这显然是行不通的)。

除了以上所有内容之外,我还能用 Twitter 做什么?它有一个昵称字段。如何将单词分开,以便第一个单词保存为 first_name,第二个单词保存为 last_name?

从下面特别尖刻的评论中推断,我再次尝试了这个建议,使用 if 语句。它仍然不起作用。

def self.find_for_oauth(auth, signed_in_resource = nil)

    # Get the identity and user if they exist
    identity = Identity.find_for_oauth(auth)

    # If a signed_in_resource is provided it always overrides the existing user
    # to prevent the identity being locked with accidentally created accounts.
    # Note that this may leave zombie accounts (with no associated identity) which
    # can be cleaned up at a later date.
    user = signed_in_resource ? signed_in_resource : identity.user

    # Create the user if needed
    if user.nil?

      # Get the existing user by email if the provider gives us a verified email.
      # If no verified email was provided we assign a temporary email and ask the
      # user to verify it on the next step via UsersController.finish_signup
      email_is_verified = auth.info.email && (auth.info.verified || auth.info.verified_email)
      email = auth.info.email if email_is_verified
      user = User.where(:email => email).first if email

      # Create the user if it's a new registration
      if user.nil?
        user = User.new(
          if auth.provider = linkedin
              first_name: auth.extra.raw_info.firstName,
              last_name: auth.extra.raw_info.lastName,
              email: emailAddress ? email : "#{auth.uid}-#{auth.provider}.com",
              #username: auth.info.nickname || auth.uid,
              # email: email ? email : "#{auth.uid}-#{auth.provider}.com",
              password: Devise.friendly_token[0,20]
          end

          if auth.provider = facebook    
              first_name: auth.extra.raw_info.first_name,
              last_name: auth.extra.raw_info.last_name,
              email: auth.extra.raw_info.email ? email : "#{auth.uid}-#{auth.provider}.com",
              password: Devise.friendly_token[0,20]

          end

          if auth.provider = twitter
            first_name: auth.extra.raw_info.firstName, 
          end
        )
        user.skip_confirmation!
        user.save!
      end
    end

由于没有在此板上找到解决方案,我希望您能就您认为支付专业人士帮助解决这些问题的合理费用提出建议。

我已经返回并注释掉了与设计和 OmniAUTH 相关的所有代码,现在再次尝试,OMNIAUTH Wiki 上的文档称为:管理多个提供者

看来这个文档可能有经验丰富的编码人员可以阅读过去的错别字。

目前,正在生成如下错误消息:

/Users/config/routes.rb:35: syntax error, unexpected [, expecting keyword_do or '{' or '(' ...', to: 'sessions#create', via [:get, :post] ... ^ /Users/config/routes.rb:36: syntax error, unexpected [, expecting keyword_do or '{' or '(' match '/logout', to: 'sessions#destroy', via [:get, :post] ^

我直接从用户指南中复制了这些路线。我远不是专家,但我也很困惑为什么在这个文档中的某些地方使用了“==”而在其他地方使用了“=”。例如:

if signed_in?
            if @identity.user == current_user

同时:

@identity.user = current_user

在相同的方法中存在差异。

我也很困惑为什么会话控制器不从设计控制器继承。在我完成的其他每个教程中,都有一个会话控制器,它继承自 devise。

本教程还有很多其他令人困惑的方面(例如为什么它没有注册控制器,为什么没有其他设计路线,为什么应用程序控制器有创建和销毁方法)?

急切地寻求帮助。

4

2 回答 2

0

这不是一个完整的答案,但我已经解决了部分问题。

oauth gem 的重点是从每个社交媒体策略中获取属性,并将它们统一为一种通用的表达形式。

通过从策略中合并 raw_info,代码不能与 oauth 一起使用。例如,linkedin auth hash 返回标记为“firstName”的数据,其中 oauth 将其识别为 first_name。如果你使用

first_name: auth.extra.raw_info.first_name,

调用链接时,该属性将为 nil。这对我来说已经很奇怪了,因为 LinkedIn 提供了基本的个人资料详细信息,表明标签是名字。

无论如何,我修复的部分是删除上述行中对“extra.raw”的引用并使用 auth.info.first_name。这应该可以解决策略标签之间的差异。

我仍在努力解决此设置中出现的其他问题。sourcey 教程中的一些问题是语法问题,其他问题更重要。如果我可以整理它们,我会再次发布。

于 2015-12-09T19:21:57.670 回答
-1
unknown attribute 'name' for User.

这意味着表中没有nameusers。您需要创建此列或使用您拥有的其他列(如first_nameusername)。像这样:

username: auth.extra.raw_info.name

但是,如果 Facebook 或 twitter 再次使用不同的字段名称,本教程如何解决这个问题?

是的,本教程只提供了一种变体,但其他变体并不难找到。您只需要知道,哪些字段会返回这个或那个提供者(twitter、facebook 或linkedin)并执行以下操作:

def self.find_for_oauth(auth, signed_in_resource = nil)
  ...
  # Create the user if needed
  if user.nil?
    case auth.provider
      when 'facebook'
        # Take fields that Facebook provides and use them when creating a new user
      when 'twitter'
        # Take fields that Twitter provides and use them when creating a new user
      when 'linkedin'
        # Take fields that Linkedin provides and use them when creating a new user
    end
  end
  ...
end

如果您为每个提供者实现单独的方法并在某些when.

另请查看https://github.com/intridea/omniauth/wiki/Auth-Hash-Schema以了解其中的内容env['omniauth.auth']

于 2015-12-07T10:31:09.980 回答