0

I've been trying to solve this problem for the past three days but to no avail. All I'm looking to do is allow users who logged in using Twitter to tweet to their accounts.

Here's the error message that I'm trying to resolve:

Twitter::Error::Unauthorized in PostsController#create
Invalid or expired token

I've tried all sorts of different things but I came back a full 360 circle to the same message.

Here's where I'm at right now:

class ApplicationController < ActionController::Base

   def twitter
    unless @twitter_user
      provider = Authentication.find_by_provider('twitter')
      @twitter_user = Twitter::Client.new(:token => provider.token, :secret => provider.secret) rescue nil
    end
    @twitter_user
    end

Here's where the error is thrown

class PostsController < ApplicationController
    def create
        twitter.update(@post.content)
    end

Here's how the schema is laid out

create_table "authentications", :force => true do |t|
    t.integer  "user_id"
    t.string   "provider"
    t.string   "uid"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
    t.string   "secret"
    t.string   "token"
  end

  create_table "users", :force => true do |t|
    t.string    "name"
    t.string    "email"
    t.timestamp "created_at",                         :null => false
    t.timestamp "updated_at",                         :null => false
    t.string    "password_digest"
    t.string    "remember_token"

Here's my initializer

Rails.application.config.middleware.use OmniAuth::Builder do
  configure do |config|
    config.path_prefix = '/auth'
  end

  provider :twitter, "XXXXXXXXXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
end

Twitter.configure do |config|
  config.consumer_key = "XXXXXXXXXXXXXXXXXXXXXX"
  config.consumer_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
  config.oauth_token = :token
  config.oauth_token_secret = :secret
end

What am I doing wrong?

fmendez
  • 7,250
  • 5
  • 36
  • 35
johbones
  • 199
  • 13
  • Didn't you asked the same question today? http://stackoverflow.com/questions/15964676/twittererrorunauthorized-in-postscontrollercreate-invalid-or-expired-toke – fmendez Apr 12 '13 at 18:36
  • I tried all of the methods posted in that thread plus more and I'm really hoping to solve this soon. That was posted yesterday – johbones Apr 12 '13 at 18:45
  • Did you confirmed that the credentials you're using are actually valid? – fmendez Apr 12 '13 at 18:49
  • Are you sure you're not persisting the wrong token? Tokens do expire and occasionally you'll need to ask for another one. All the schema and the controller and all this other code is basically irrelevant as far as I can tell. – jefflunt Apr 12 '13 at 18:50
  • well, users are able to sign up/login correctly with no issues so doesn't that confirm that the token+secret are valid? – johbones Apr 12 '13 at 18:50
  • Are you only talking about new users, or existing users? – jefflunt Apr 12 '13 at 18:51
  • @normalocity, I researched into this yesterday as well as today and it says Twitter tokens do not expire. I'm talking about new and existing users when they try to post – johbones Apr 12 '13 at 18:51
  • fmendez... that fixed it. thank you SO much. If you want to post it as an answer, I will accept it – johbones Apr 12 '13 at 18:55
  • @johbones Btw, if you are already using an initialiser, why are you passing the credentials at creation? You should be able to just say: `Twitter::Client.new` – fmendez Apr 12 '13 at 18:56
  • @fmendez, I may have to create a new thread on this later today, but do you know why my code is allowing everyone (whether they're authenticated using omniauth or not) to post to the same twitter account? I believe it may have something to do with way `def twitter` is set up – johbones Apr 12 '13 at 19:19

1 Answers1

0

Try changing this:

Twitter::Client.new(:token => provider.token, :secret => provider.secret) 

into this

Twitter::Client.new(:oauth_token => provider.token, :oauth_token_secret => provider.secret)
fmendez
  • 7,250
  • 5
  • 36
  • 35