我正在创建 Rails 应用程序,它将允许我的 Twitter 应用程序(来自我的 Twitter 帐户)发布到用户的时间线。目前我的网络应用程序中有这些设置应用程序:
consumer_key
consumer_secret
access_token
access_token_secret
目前,当用户登录我的网络应用程序时,会创建一些推文并发布它们。这些帖子转到我的 Twitter 帐户。在授权回调之后,我的网络应用程序能够设置secret
并token
从回调返回到用户帐户。我需要设置哪些密钥和令牌才能使我的网络应用在授权后发布到另一个用户的帐户?
我正在使用从 twitter 返回一些哈希值的 gem:https ://github.com/arunagw/omniauth-twitter#authentication-hash 。
我的代码如下所示:config/secrets.yml
development:
secret_key_base: 69cc9e7ad45eadafe574hsaf5ba04a21c8bbe9337fb0cb73966bde47a397d72dd0963b29f6218d43c1baeca8bd0a74218e2e552c8d2ab5fdb7fef14
twitter_api_key: 2BN4jYtsf453374VM4m7HG
twitter_api_secret: LOE545ksfjkae3r8FOzj3NTHXVBfx8njPX0JHMNqCJs3mOnhQleH
test:
secret_key_base: 4c644a9f6d038388ec8ds3060177db7a6c342e4d12083cd36b0b7c3b6609eeb6771681c651c3eee3d740f9003103592sdafdsfaeDFHDGAd6c3e938bf
# Do not keep production secrets in the repository,
# instead read values from the environment.
production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
配置/初始化程序/twitter.rb
$twitter = Twitter::REST::Client.new do |config|
config.consumer_key = "2BN4jYtsf453374VM4m7HG"
config.consumer_secret = "LOE545ksfjkae3r8FOzj3NTHXVBfx8njPX0JHMNqCJs3mOnhQleH"
config.access_token = "714265291988058112-EkpjQvSeO6fQBc1ksHahlL8r6QLsYinA"
config.access_token_secret = "qFdiOIP6aozmBlPfsUEYvDK884jsfaehwOXqlFSf8J4g8bl0x"
end
帐号.rb
class Account < ActiveRecord::Base
before_save :capitalize_name
belongs_to :user
has_many :posts, dependent: :destroy
accepts_nested_attributes_for :posts
validates :account_name, :description, presence: :true
default_scope { order('created_at DESC') }
def self.find_or_create_from_auth_hash(auth_hash)
#account = where(provider: auth_hash.provider, uid: auth_hash.uid).first_or_create
account = where(id: id_goes_here).first_or_create
account.update_attributes!(
name: auth_hash.info.name,
profile_image: auth_hash.info.image,
token: auth_hash.credentials.token,
secret: auth_hash.credentials.secret,
uid: auth_hash.uid
)
account
end
private
def capitalize_name
self.account_name = self.account_name.split.map { |name| name.capitalize }.join(" ")
end
end
目前,即使其他用户登录、获得授权并通过授权回调在他们的帐户上设置令牌和机密,我的 rails 应用程序始终会发布到我的帐户。这不是我想要的。
PS:这里发布的钥匙是假的。