0

我从 Twitter 上得到了似乎是成功的回应:

/auth/twitter/callback?oauth_token=somelongtoken&oauth_verifier=someverifier

但是那里没有oauth_token_secret。我在哪里得到它?

细节

routes.rb

get '/auth/:provider', to: 'authorisations#authorise', :as => :new_auth
get '/auth/:provider/callback', to: 'authorisations#callback'

authorisations_controller.rb

def authorise
  session[:user_id] = current_user.id
  @authorisation = Authorisation.new
  @authorisation.user_id = current_user.id
  if auth_hash.provider == "facebook"
    @authorisation.provider = auth_hash.provider
    @authorisation.oauth_token = auth_hash.credentials.token
    @authorisation.oauth_expires_at = Time.at(auth_hash.credentials.expires_at)
  elsif params[:provider] == "twitter"
    @authorisation.provider = params[:provider]
    @authorisation.oauth_token = params[:oauth_token]
    @authorisation.oauth_token_secret = params[:oauth_token_secret]
    @authorisation.access_token = params[:oauth_verifier]
  end
  @authorisation.save!
end

def callback
  session[:user_id] = current_user.id
  @authorisation = Authorisation.new
  @authorisation.user_id = current_user.id
  if auth_hash.provider == "facebook"
    @authorisation.provider = auth_hash.provider
    @authorisation.oauth_token = auth_hash.credentials.token
    @authorisation.oauth_expires_at = Time.at(auth_hash.credentials.expires_at)
  elsif params[:provider] == "twitter"
    @authorisation.provider = params[:provider]
    @authorisation.oauth_token = params[:oauth_token]
    @authorisation.oauth_token_secret = params[:oauth_token_secret]
    @authorisation.access_token = params[:oauth_verifier]
  end
  @authorisation.save!
  redirect_to root_url, notice: "#{current_user.name} and #{params[:provider].titlecase} have been linked."
end

def auth_hash
  request.env['omniauth.auth']
end

documents_controller.rb

def twitter
  session[:return_to] = request.referer
  @document = Document.find(params[:id])
  if @document.template.name == "Image"
    @document.process_social_media
    twitter_user.update_with_media("#{@document.remove_html(@document.components.first.body[0..100])}...", "#{root_url}temporary#{@document.temp_file_name}.jpg")
  else
    twitter_user.update("#{@document.remove_html(@document.components.first.body[0..100])}... #{root_url.gsub(/\/$/, '')}#{share_path(@document.user.ftp, @document)}")
  end
  redirect_to session[:return_to], notice: "#{@document.title} has been posted to Twitter."
end

def twitter_user
  user = Twitter.configure do |config|
    config.consumer_key       = ENV['TWITTER_CONSUMER_KEY']
    config.consumer_secret    = ENV['TWITTER_CONSUMER_SECRET']
    config.oauth_token        = current_user.single_authorisation("twitter").oauth_token
    config.oauth_token_secret = current_user.single_authorisation("twitter").oauth_token_secret
  end
end
4

2 回答 2

0

我已经有一段时间没有这样做了,所以也许它已经改变了,但这些是我在授权请求中传递的参数:

oauth_consumer_key
oauth_nonce
oauth_signature
oauth_signature_method
oauth_timestamp
oauth_version
于 2013-11-15T13:28:29.423 回答
0

它适合任何四处寻找的人:

def create
  @authorisation.oauth_token_secret = auth_hash.credentials.secret
end

def auth_hash
  request.env['omniauth.auth']
end

干杯。

于 2013-11-19T01:02:11.240 回答