Ruby on Rails中的Twitter - 500内部服务器错误:“您的凭据不允许访问此资源”

时间:2015-10-19 23:19:10

标签: ruby-on-rails ruby-on-rails-4 twitter twitter-oauth

所有

我正在通过自己教授Oauth-Twitter和Ruby on Rails中的Twitter gem来努力。尽管已经在Rails dev上完成了一个训练营,但我仍然相当新鲜。

我能够让OAuth - Twitter正常运行,但现在我正在尝试从应用程序发推文,我得到以下内容:

Twitter::Error::Forbidden at /tweets Your credentials do not allow access to this resource.

client.update(tweet)文件中user.rb会抛出错误:

class User < ActiveRecord::Base
  class << self
    def from_omniauth(auth_hash)
      user = find_or_create_by(uid: auth_hash['uid'], provider: auth_hash['provider'])
      user.name = auth_hash['info']['name']
      user.location = auth_hash['info']['location']
      user.image_url = auth_hash['info']['image']
      user.url = auth_hash['info']['urls']['Twitter']
      user.oauth_token=auth_hash.credentials.token
      user.oauth_secret=auth_hash.credentials.secret
      user.save!
      user
    end
  end

  def tweet(tweet)
    client = Twitter::REST::Client.new do |config|
      config.consumer_key = Rails.application.config.twitter_key
      config.consumer_secret = Rails.application.config.twitter_secret
      config.access_token = oauth_token
      config.access_token_secret = oauth_secret
    end
    client.update(tweet)
  end
end

我的架构:

ActiveRecord::Schema.define(version: 20151018214537) do

  create_table "users", force: :cascade do |t|
    t.string   "provider",     null: false
    t.string   "uid",          null: false
    t.string   "name"
    t.string   "location"
    t.string   "image_url"
    t.string   "url"
    t.datetime "created_at",   null: false
    t.datetime "updated_at",   null: false
    t.string   "oauth_token"
    t.string   "oauth_secret"
  end

  add_index "users", ["provider", "uid"], name:    "index_users_on_provider_and_uid", unique: true
  add_index "users", ["provider"], name: "index_users_on_provider"
  add_index "users", ["uid"], name: "index_users_on_uid"

end

你能提供的任何帮助都很棒。您可以在Github Repo

找到所有其他信息

1 个答案:

答案 0 :(得分:0)

在一次协助后,我发现我没有访问我在application.yml中存储的密钥(yay,Figaro!)。 user.rb已更新为:

  def tweet(tweet)
    client = Twitter::REST::Client.new do |config|
      config.consumer_key = ENV['TWITTER_CONSUMER_KEY']
      config.consumer_secret = ENV['TWITTER_CONSUMER_SECRET']
      config.access_token = ENV['TWITTER_ACCESS_TOKEN']
      config.access_token_secret = ENV['TWITTER_ACCESS_SECRET']
    end
    client.update(tweet)
  end
end

通过我的帐户测试Tweet工作得很好。