I'm trying to integrate my app with twitter API.
here are my steps:
1: installed the twitter gem: gem 'twitter
2:grabbed this code from the twitter gem documentation and placed it inside my controller. I also tried placing the code in a helper.
require 'twitter'
@client = Twitter::REST::Client.new do |config|
config.consumer_key = ENV["CONSUMER_KEY"]
config.consumer_secret = ENV["CONSUMER_SECRET"]
config.access_token = ENV["ACCESS_TOKEN"]
config.access_token_secret = ENV["ACCESS_SECRET"]
end
3) registered my app with twitter and got the consumer_key (AKA: API key), consumer_secret (AKA:API secret), access_token, and access_token_secret.
I used a config/application.yml to store access tokens. is that correct way of doing it? I hope so.
4) in views, I have <%= @client.user.username %>.
I'm doing this in development (localhost3000). my callback url is http://127.0.0.1/
I also tried to organize my code as this answer suggests to no avail.
I'm using Rails 4.0.2
I tested the code in the console and is working perfectly. I tried several queries including the one shown above: <%= @client.user.username %> and they are all working.
but when I run my code in development i get this error:
undefined method `user'` for nil:NilClass.
I understand what the error means. but how is it possible that @client is nil? when I run it in the console, it is not nil. Did I forget something? I would appreciate any suggestions. Please let me know if you would like me to provide more info/code. Thanks.
EDIT: Also, tried to move the above code that start with @client to a config/initializers/twitter.rb. Getting same error message.