Why is the twitter oauth access token invalid / ex

2019-08-20 07:38发布

问题:

This question is an exact duplicate of:

  • Twitter::Error::Unauthorized in PostsController#create - Invalid or expired token 2 answers

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?

回答1:

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)