为什么twitter oauth访问令牌无效/过期(rails 3)

时间:2013-04-12 18:24:01

标签: ruby-on-rails ruby omniauth twitter-oauth

过去三天我一直试图解决这个问题,但无济于事。我所要做的就是允许使用Twitter登录的用户向他们的帐户发推文。

以下是我要解决的错误消息:

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

我尝试了各种不同的东西但是我回到了同一个消息的完整360圈。

这就是我现在所处的位置:

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

这是抛出错误的地方

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

以下是架构的布局方式

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"

这是我的初始化程序

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

我做错了什么?

1 个答案:

答案 0 :(得分:0)

尝试更改此内容:

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

进入这个

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