2

这可能很愚蠢,但我已经用谷歌搜索并经历了堆栈溢出,并且在浪费了几个小时后没有发现任何运气。

基本上,我克隆并部署了这个 - https://github.com/alex-klepa/rails4-bootstrap-devise-cancan-omniauth并没有改变任何东西(除了放入我的消费者密钥和秘密)。

我能够使用我的 twitter 应用程序和 facebook 应用程序凭据登录并运行它。我遇到问题的地方是使用 twitter gem 和 fb_graph gem 以及omniauth 创建并存储在属于_to 的身份模型中的凭据用户模型。

似乎已经为用户进行了会话管理-为该用户生成的令牌和秘密存储在身份模型中,但我仍然收到“您的凭据不允许访问此资源”。

长话短说,这是推特配置:

Twitter.configure do |config|
    config.consumer_key = 'yxxxxxx'
    config.consumer_secret = 'kxxxxxxx'
    config.oauth_token = ['need help here']
    config.oauth_token_secret = ['need help here']
end

我希望将一些动态的东西放入依赖于当前用户会话的 oauth_token 和 oauth_token_secret 字段中,这样我就可以将 API 调用放入我的视图中。

提前感谢您能给我的任何帮助!

编辑:

我突然想到这些模型可能会有所帮助。(其他一切都在 git 链接中) *还有另外两个支持模型,auth_definitions.rb roles.rb,它们支持 Devise,但在这里似乎没有任何关系。

user.rb
    class User
      include Mongoid::Document
      include Mongoid::Timestamps
      include User::AuthDefinitions
      include User::Roles

      has_many :identities


      field :email, type: String
      field :image, type: String
      field :first_name, type: String
      field :last_name, type: String
      field :roles_mask, type: Integer

      validates_presence_of :email, :first_name, :last_name

      def full_name
        "#{first_name} #{last_name}"
      end

    end

身份.rb

class Identity
  include Mongoid::Document
  include Mongoid::Timestamps

  belongs_to :user, index: true

  field :uid, type: String
  field :provider, type: String
  field :token, type: String
  field :secret, type: String
  field :expires_at, type: DateTime

  field :email, type: String
  field :image, type: String
  field :nickname, type: String
  field :first_name, type: String
  field :last_name, type: String

  index({ uid: 1, provider: 1 }, { unique: true })


  def self.from_omniauth(auth)
    identity = where(auth.slice(:provider, :uid)).first_or_create do |identity|
      identity.provider     = auth.provider
      identity.uid          = auth.uid
      identity.token        = auth.credentials.token
      identity.secret       = auth.credentials.secret if auth.credentials.secret
      identity.expires_at   = auth.credentials.expires_at if auth.credentials.expires_at
      identity.email        = auth.info.email if auth.info.email
      identity.image        = auth.info.image if auth.info.image
      identity.nickname     = auth.info.nickname
      identity.first_name   = auth.info.first_name
      identity.last_name    = auth.info.last_name
    end
    identity.save!

    if !identity.persisted?
      redirect_to root_url, alert: "Something went wrong, please try again."
    end
    identity
  end

  def find_or_create_user(current_user)
    if current_user && self.user == current_user
      # User logged in and the identity is associated with the current user
      return self.user
    elsif current_user && self.user != current_user
      # User logged in and the identity is not associated with the current user
      # so lets associate the identity and update missing info
      self.user = current_user
      self.user.email       ||= self.email
      self.user.image       ||= self.image
      self.user.first_name  ||= self.first_name
      self.user.last_name   ||= self.last_name
      self.user.skip_reconfirmation!
      self.user.save!
      self.save!
      return self.user
    elsif self.user.present?
      # User not logged in and we found the identity associated with user
      # so let's just log them in here
      return self.user
    else
      # No user associated with the identity so we need to create a new one
      self.build_user(
        email: self.email,
        image: self.image,
        first_name: self.first_name,
        last_name: self.last_name,
        roles: [AppConfig.default_role]
      )
      self.user.save!(validate: false)
      self.save!
      return self.user
    end
  end

  def create_user

  end
end
4

1 回答 1

4

碰巧我几天前就按照你的要求做了。第一件事是在回调从twitter返回后将用户的令牌和秘密存储在会话哈希中,在我的例子中是:

omn​​i_callbacks_controller.rb:

session[:token] = request.env["omniauth.auth"].credentials.token
session[:secret] = request.env["omniauth.auth"].credentials.secret

之后,您只需要在 Twitter.config 中设置消费者凭据(同时请编辑您的消费者的令牌和秘密!重要的是不要向全世界展示这些信息):

Twitter.configure do |config|
    config.consumer_key = APP_TOKEN
    config.consumer_secret = APP_SECRET
end

那么您所要做的就是创建 Twitter.client,在会话哈希中传递用户的令牌和秘密存储:

client = Twitter::Client.new(oauth_token: session[:token], oauth_token_secret: session[:secret])
client.update("This sends a message to user's feed on twitter")
于 2013-09-01T03:29:04.237 回答