Misanthropos
Misanthropos

Reputation: 71

undefined local variable or method `oauth_token' when sending Tweets from Rails App

I'm currently working on sharpening my RoR skills after spending three months learning the basics through a bootcamp. I'm slowly building concept upon concept with an app I'm working on to create a Twitter utility (been done, I know. It's been good practice).

After much trepidation I was able to get oauth-twitter up and running but now I'm starting to scratch the surface of the Twitter API and I'm having some difficulties.

I've created what I believe is all the functionality to send tweets directly from my application but, upon hitting send I get this error:

NameError at /tweets
undefined local variable or method 'oauth_token' for #<User:0x007f821a6b66d0>`

It's throwing it off this block of code:

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

With the error being highlighted at config.access_token = oauth_token

The server stack trace gives me this:

NoMethodError - undefined method `consumer_key' for #    <Rails::Application::Configuration:0x007f82182b9188>:
  railties (4.2.3) lib/rails/railtie/configuration.rb:95:in     `method_missing'
  app/models/user.rb:16:in `block in tweet'
  twitter (5.15.0) lib/twitter/client.rb:23:in `initialize'
  app/models/user.rb:15:in `tweet'

If anyone could take a look at this and give me some feedback I'd much appreciate it. You can check out the branch in my repo where this is all stored here: InsomniaNoir - Project: :kronoTweeter

Thanks in advance!

Upvotes: 0

Views: 498

Answers (1)

devor
devor

Reputation: 195

You have not both oauth_token and oauth_secret in users table. You need create new migration like this:

rails g migration AddOauthFieldsToUsers oauth_token oauth_secret

after this don't forget run

rake db:migrate

then should be modified from_omniauth method in User class

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

Upvotes: 1

Related Questions