【问题标题】:Where to put auth information在哪里放置身份验证信息
【发布时间】:2013-04-24 14:17:13
【问题描述】:

所以我在 VisualRuby 中制作了一个可以发推文的小程序。所以这就是我的main.rb 的样子:

#!/usr/bin/ruby

require 'vrlib'
require 'twitter_oauth'

#make program output in real time so errors visible in VR.
STDOUT.sync = true
STDERR.sync = true

#everything in these directories will be included
my_path = File.expand_path(File.dirname(__FILE__))
require_all Dir.glob(my_path + "/bin/**/*.rb") 

LoginWindow.new.show

我的LoginWindow.rb 看起来像这样

require 'twitter_oauth'

class LoginWindow #(change name)

    include GladeGUI

    client = TwitterOAuth::Client.new(
        :consumer_key => '****',
        :consumer_secret => '****',
        :token => '****-****',
        :secret => '****'
    )

    def show()
        load_glade(__FILE__)  #loads file, glade/MyClass.glade into @builder
        set_glade_all() #populates glade controls with insance variables (i.e. Myclass.label1) 
        show_window() 
    end 

    def button1__clicked(*argv)
        if client.authorized? 
            puts "true"
        end
    end

end

最后我的窗口看起来像这样:

现在,当我运行它并单击登录按钮时,VR 将其吐出

C:/Users/*/visualruby/Test/bin/LoginWindow.rb:22:in `button1__clicked': undefined local variable or method `client' for #<LoginWindow:0x3f56aa8>
     from C:/Ruby193/lib/ruby/gems/1.9.1/gems/vrlib-0.0.33/lib/GladeGUI.rb:146:in `call'
     from C:/Ruby193/lib/ruby/gems/1.9.1/gems/vrlib-0.0.33/lib/GladeGUI.rb:146:in `block (3 levels) in parse_signals'
     from C:/Ruby193/lib/ruby/gems/1.9.1/gems/vrlib-0.0.33/lib/GladeGUI.rb:331:in `call'
     from C:/Ruby193/lib/ruby/gems/1.9.1/gems/vrlib-0.0.33/lib/GladeGUI.rb:331:in `main'
     from C:/Ruby193/lib/ruby/gems/1.9.1/gems/vrlib-0.0.33/lib/GladeGUI.rb:331:in `show_window'
     from C:/Users/*/visualruby/Test/bin/LoginWindow.rb:17:in `show'
     from main.rb:14:in `<main>'

我认为我不应该将 client = Twitter.... 的东西放在 LoginWindow 类中,但我想不出其他任何地方可以放置它。有关如何解决此问题的任何想法?

【问题讨论】:

  • 也许您应该在实例变量中转换客户端。您可以在初始化方法中调用 TwitterOAuth。
  • @amalrikmaia 你到底是什么意思?你是说有一个初始化函数,里面有一个实例变量@client
  • 你是说有一个初始化函数,里面有一个实例变量@client?是的。
  • @amalrikmaia 这行得通,这么简单的解决方案不知道为什么我没有想到它。请将此作为答案发布,我会批准它,非常感谢!

标签: ruby authentication user-interface twitter rubygems


【解决方案1】:

这是满足您需要的快速解决方案。 在你的 LoginWindow.rb

def initialize
   @client = TwitterOAuth::Client.new( 
       :consumer_key => '****',
       :consumer_secret => '****',
       :token => '****-****',
       :secret => '****'
   )
end


def button1__clicked(*argv)
    if @client.authorized? 
        puts "true"
    end
end

这个解决方案的问题是现在你不能在没有初始化LogginWindow的情况下调用button1_clicked,所以要小心。

【讨论】:

    猜你喜欢
    • 2012-07-22
    • 2016-05-23
    • 2020-03-03
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-25
    • 1970-01-01
    相关资源
    最近更新 更多