0

我正在尝试使用 OmniAuth 来处理小型 Sinatra 应用程序的 OAuth 流程。我可以让 37signals Oauth 完美运行,但我也在尝试为 Freshbooks Oauth 创建策略。

不幸的是,Freshbooks 需要 OAuth 请求才能转到用户特定的子域。我正在获取子域作为输入,然后我需要对所有请求持续使用客户特定的站点 URL。

这是我到目前为止所尝试的。问题是新站点值在第一个请求之后不会持续存在。

必须有一种简单的方法来实现这一点,但我很难过。

  #Here's the setup -
  def initialize(app, consumer_key, consumer_secret, subdomain='api')
    super(app, :freshbooks, consumer_key, consumer_secret,
               :site               => "https://"+subdomain+".freshbooks.com", 
               :signature_method   => 'PLAINTEXT',
               :request_token_path => "/oauth/oauth_request.php",
               :access_token_path  => "/oauth/oauth_access.php",
               :authorize_path     => "/oauth/oauth_authorize.php"
          )
  end


  def request_phase
    #Here's the overwrite -
    consumer.options[:site] = "https://"+request.env["rack.request.form_hash"]["subdomain"]+".freshbooks.com"
    request_token = consumer.get_request_token(:oauth_callback => callback_url)
    (session[:oauth]||={})[name.to_sym] = {:callback_confirmed => request_token.callback_confirmed?, 
                                           :request_token => request_token.token, 
                                           :request_secret => request_token.secret}
    r = Rack::Response.new
    r.redirect request_token.authorize_url
    r.finish
  end
4

1 回答 1

0

好的,这是我为通过 Google 遇到此问题的任何人所做的总结。

我没有按照我要求的方式解决问题,而是将子域推送到会话中,然后在需要使用站点值时覆盖它。

这是代码:

  #Monkeypatching to inject user subdomain
  def request_phase
    #Subdomain is expected to be submitted as <input name="subdomain">
    session[:subdomain] = request.env["rack.request.form_hash"]["subdomain"]
    consumer.options[:site] = "https://"+session[:subdomain]+".freshbooks.com"
    super 
  end

  #Monkeypatching to inject subdomain again
  def callback_phase
    consumer.options[:site] = "https://"+session[:subdomain]+".freshbooks.com"
    super
  end

请注意,您仍然必须在初始化时将某些内容设置为站点,否则由于 OAuth 未使用 SSL 发出请求,您将收到错误消息。

如果您想查看我正在使用的实际代码,请访问:https ://github.com/joeharris76/omniauth一旦我对这个解决方案进行了更多的战斗测试,我将把叉子推到主项目上。

于 2010-11-08T16:41:33.197 回答