Twitter Rails gem "Unable to verify your credentials",初始化器不工作

Twitter Rails gem "Unable to verify your credentials", Initializer not working

我正在使用 Twitter Gem and Figaro 但我的凭据没有被存储。这是我的设置:

config/initializers/twitter.rb

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

config/application.yml:

TWITTER_CONSUMER_KEY: "12345"
TWITTER_CONSUMER_SECRET: "12345"
TWITTER_ACCESS_TOKEN: "12345"
TWITTER_ACCESS_SECRET: "12345"

在视图中放置以下内容会导致 "Unable to verify your credentials" 错误,我认为这是由初始化程序未正确存储我的凭据引起的。

<%=
@client =  Twitter::REST::Client.new
@client.user_timeline("cnn") %>

我试过将各种项目放入控制器中,但没有任何效果。通过控制台:

client =  Twitter::REST::Client.new
=> #<Twitter::REST::Client:0x007fadf06364b0>
client.consumer_key
 => nil

非常感谢任何帮助,谢谢

client = Twitter::REST::Client.new 正在创建一个没有参数的新对象;它没有重复使用 config/initializers/twitter.rb

中配置的内容

尝试将控制器中的初始化代码和访问代码放在一起,如下所示:

@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.user_timeline("cnn")