2011-09-01 5 views
1

저는 최신 Rails 앱에서 Twitter 젬을 사용해 왔지만 지금까지 아무런 문제가 없었습니다. 앱을 등록하고 config/initializers/twitter.rb에 API 토큰을 설정하고 보석이 필요한 사용자 정의 레이크 테스트에서 작동하는지 테스트했습니다. 그러나 트위터 양식 컨트롤러를 보내려고하면 아무 일도 일어나지 않는다는 것이 문제입니다. 내 초기화 그래서 다음과 같습니다트위터의 보석이 Rails의 컨트롤러에서 작동하지 않습니다.

require 'twitter' 

Twitter.configure do |config| 
    config.consumer_key = '###' 
    config.consumer_secret = '###' 
    config.oauth_token = '###' 
    config.oauth_token_secret = '###' 
end 

###는 분명, 내 응용 프로그램에서 제대로 채워집니다. 내 레이크 파일에서 필자는 파일 상단에있는 보석을 필요로하고 Twitter.update(tweet)으로 테스트 트윗을 보낼 수 있지만 컨트롤러에서 동일한 구문이 작동하지 않습니다.

내가 뭘 잘못하고 있니? 컨트롤러에서 보석을 다시 초기화해야합니까?

+0

약간의 땜질 후에, 나는 그것을 스스로 알아 냈다. 아래를 참조하십시오. – bjork24

답변

2

는 일부 땜질 후,이 간단한 솔루션입니다. 그건 그렇고, 사용자가 앱을 통해 트윗을 보내는 것이 아니라, 트윗을 보내는 앱이므로 재 인증 할 필요가 없습니다.

1

나는 또한 트위터 보석을 사용하고 있으며 트위터 DM에 대한 내 서약, 직접 메시지 컨트롤러 및 아약스를 사용하고 권한 부여 컨트롤러를 사용하고 있습니다. AppConfig는 내 cred가 포함 된 yml 파일입니다.

authorizations_controller.rb

class AuthorizationsController < ApplicationController 
    def new 
    set_oauth 
    render :update do |page| 
     page.redirect_to @oauth.request_token.authorize_url 
    end 
    end 

    def show 
    @oauth ||= Twitter::OAuth.new(AppConfig['consumer']['token'], AppConfig['consumer']['secret']) 
    @oauth.authorize_from_request(session['rtoken'], session['rsecret'], params[:oauth_verifier]) 

    session['rtoken'] = nil 
    session['rsecret'] = nil 
    session['atoken'] = @oauth.access_token.token 
    session['asecret'] = @oauth.access_token.secret 
    redirect_path = session['admin'] ? admin_tweets_path : root_path 
    redirect_to redirect_path 
    end 
end 

direct_messages_controller.rb

class DirectMessagesController < ApplicationController 
    before_filter :authorize 

    def create 
    @client.update("@#{AppConfig['user']} #{params[:tweet][:text]}") 

    render :update do |page| 
     page.replace_html 'tweet_update', "Your tweet has been sent to #{AppConfig['user']} and should be updated momentarily." 
    end 
    end 
end 

view.html.haml 내 필터 전에 단지 세션 확인 '인증'

#tweet_update 
    - form_remote_tag :url => direct_messages_url, :method => :post, :loading => "$('tweet_update').hide();$('loading').show()", :complete => "$('tweet_update').show();$('loading').hide()" do 
    %div{:class => "subheader float_left"}Tweet to Whoever 
    - if session_set? 
     %input{:type => "image", :src=>"/images/sendButton.jpg", :class =>"float_right", :style=>"margin-bottom: 4px"} 
    - else 
     %div{:class => "float_right", :id => "twitter_login_button"}= link_to_remote image_tag('twitter-darker.png'), :url => new_authorization_url, :method => :get 
    .float_clear 
    #tweetbox_bg 
     - textarea_options = {:id => "tweetbox", :style => "overflow: auto", :rows => "", :cols => ""} 
     - textarea_value = nil 
     - unless session_set? 
     - textarea_options.merge!(:disabled => "disabled") 
     - textarea_value = "Please login to tweet Whoever!" 

     = text_area_tag 'tweet[text]', textarea_value, textarea_options 

:

희망이 도움이됩니다. 앱을 시작했을 때 트위터 클라이언트가 이미 인증 된했기 때문에 내 컨트롤러 방법으로, 완벽하게 작동하는 것이 추가

@twitter = Twitter::Client.new 
@twitter.update(tweet) 

:

관련 문제