如何判断 Tumblr OAuth 凭据是否有效?

发布于 2024-11-19 15:30:49 字数 1335 浏览 5 评论 0原文

使用 ruby​​ OAuth 库,我尝试验证以前用户的凭据是否仍然有效。 (以防他撤销了我的应用程序的权限)我已经为 Twitter 做到了这一点,没有任何问题,因为有一个方便的 .authenticated? Twitter OAuth 库中的方法。但是使用通用库,我找不到类似的方法。

@consumer = OAuth::Consumer.new(
        ENV['TUMBLR_CONSUMER_KEY'],
        ENV['TUMBLR_CONSUMER_SECRET'],
        { :site => 'http://www.tumblr.com',
          :request_token_path => '/oauth/request_token',
          :authorize_path => '/oauth/authorize',
          :access_token_path => '/oauth/access_token',
          :http_method => :get
        }
      )
      @access_token = OAuth::AccessToken.new(@consumer, @tumblrauth['access_token'], @tumblrauth['access_token_secret'])
      puts @access_token.inspect
      @resp = @access_token.get('api/read')
      puts @resp.inspect

当我尝试 @access_token.get() 调用时,我收到服务器错误,关于 nill 没有小写方法的问题,我猜它需要某种字符串。但我不知道它指的是什么参数。

我使用的 access_token 和机密以前可以正常运行,但我撤销了对应用程序的访问权限以测试如何检测它。我只需要某种方法来知道用户不再注册该服务,这样我就可以切换状态。

以下是堆栈跟踪:

NoMethodError - nil:NilClass 的未定义方法“downcase”: /Library/Ruby/Gems/1.8/gems/oauth-0.4.4/lib/oauth/request_proxy/base.rb:93:in 'normalized_uri' /Library/Ruby/Gems/1.8/gems/oauth-0.4.4/lib/oauth/request_proxy/base.rb:113:in 'signature_base_string' /Library/Ruby/Gems/1.8/gems/oauth-0.4.4/lib/oauth/signature/base.rb:77:in 'signature_base_string'

Using the ruby OAuth library, I am trying to verify if a previous user's credentials are still valid. (in case he has revoked privileges to my app) I've done it for Twitter with no problem as there is a handy .authenticated? method in the Twitter OAuth library. Using the generic library though, I can find no similar methods.

@consumer = OAuth::Consumer.new(
        ENV['TUMBLR_CONSUMER_KEY'],
        ENV['TUMBLR_CONSUMER_SECRET'],
        { :site => 'http://www.tumblr.com',
          :request_token_path => '/oauth/request_token',
          :authorize_path => '/oauth/authorize',
          :access_token_path => '/oauth/access_token',
          :http_method => :get
        }
      )
      @access_token = OAuth::AccessToken.new(@consumer, @tumblrauth['access_token'], @tumblrauth['access_token_secret'])
      puts @access_token.inspect
      @resp = @access_token.get('api/read')
      puts @resp.inspect

I get a server error as soon as I try the @access_token.get() call, something about nill not having a downcase method, I'm guessing it's expecting a string of some kind instead. But I don't know what param it's referring to.

The access_token and secret that I'm using were previously functional, but I revoked access to the app to test how to detect that. I just need some way to know that the user is no longer registered with that service, so I can switch the state.

Here is a stack trace:

NoMethodError - undefined method 'downcase' for nil:NilClass:
/Library/Ruby/Gems/1.8/gems/oauth-0.4.4/lib/oauth/request_proxy/base.rb:93:in 'normalized_uri'
/Library/Ruby/Gems/1.8/gems/oauth-0.4.4/lib/oauth/request_proxy/base.rb:113:in 'signature_base_string'
/Library/Ruby/Gems/1.8/gems/oauth-0.4.4/lib/oauth/signature/base.rb:77:in 'signature_base_string'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

两仪 2024-11-26 15:30:49

尝试:

@resp = @access_token.get('http://www.tumblr.com/api/read/json')

按照下面的注释进行编辑:

#Monkey Patch the Oauth gem to fit the "tumblr" way of doing Oauth
class Net::HTTP
  def address=(new_address)
    @address = new_address
  end
end

module OAuth
  class Consumer

    def tumblr_address=(address)
      @tumblr_address = address
      @orig_address = self.http.address
    end

    def create_signed_request(http_method, path, token = nil, request_options = {}, *arguments)
      # Use the original address to generate OAuth signature
      self.http.address = @orig_address if @orig_address

      request = create_http_request(http_method, path, *arguments)
      sign!(request, token, request_options)

      # Submit the request to the tumblr address
      self.http.address = @tumblr_address if @tumblr_address

      request
    end
  end
end

module OAuth
  class RequestToken
    def get_access_token(options = {}, *arguments)
      response = consumer.token_request(consumer.http_method, (consumer.access_token_url? ? consumer.access_token_url : consumer.access_token_path), self, options, *arguments)
      self.consumer.tumblr_address = response[:subdomain]
      OAuth::AccessToken.from_hash(consumer, response)
    end
  end
end

然后在 @consumer 创建后在控制器中添加以下内容:

@consumer.tumblr_address = "http://user.tumblr.com"

注意:这是未经测试的代码,所以 YMMV。

Try:

@resp = @access_token.get('http://www.tumblr.com/api/read/json')

Edit, as per comments below:

#Monkey Patch the Oauth gem to fit the "tumblr" way of doing Oauth
class Net::HTTP
  def address=(new_address)
    @address = new_address
  end
end

module OAuth
  class Consumer

    def tumblr_address=(address)
      @tumblr_address = address
      @orig_address = self.http.address
    end

    def create_signed_request(http_method, path, token = nil, request_options = {}, *arguments)
      # Use the original address to generate OAuth signature
      self.http.address = @orig_address if @orig_address

      request = create_http_request(http_method, path, *arguments)
      sign!(request, token, request_options)

      # Submit the request to the tumblr address
      self.http.address = @tumblr_address if @tumblr_address

      request
    end
  end
end

module OAuth
  class RequestToken
    def get_access_token(options = {}, *arguments)
      response = consumer.token_request(consumer.http_method, (consumer.access_token_url? ? consumer.access_token_url : consumer.access_token_path), self, options, *arguments)
      self.consumer.tumblr_address = response[:subdomain]
      OAuth::AccessToken.from_hash(consumer, response)
    end
  end
end

and then in your controller after your @consumer creation, add this:

@consumer.tumblr_address = "http://user.tumblr.com"

Note: this is untested code, so YMMV.

回眸一笑 2024-11-26 15:30:49

在没有用户 Tumblr 用户名的情况下,我可以使其正常工作的唯一方法是使用新的 v2 API。文档在这里: http://www.tumblr.com/docs/en/api/v2< /a>

@access_token.get('http://api.tumblr.com/v2/user/dashboard?limit=1')

返回一个小的 JSON 信封,其中包含 200 OK,如果令牌错误,则返回 401 Unauthorized。

现在我必须弄清楚如何获取用户的博客名称,因为您似乎无法再发布到默认博客。幸运的是,您可以同时使用 v2 和 v1 API。

The only way I could get this to work without the user's Tumblr username, is using the new v2 API. Docs here: http://www.tumblr.com/docs/en/api/v2

@access_token.get('http://api.tumblr.com/v2/user/dashboard?limit=1')

Returns a small JSON envelope with a 200 OK, or a 401 Unauthorized if the tokens are bad.

Now I have to figure out how to get the user's blog name since you can't seem to post to the default blog anymore. Fortunately, you can use both v2 and v1 APIs at the same time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文