【问题标题】:OAuth with Digg API使用 Digg API 的 OAuth
【发布时间】:2010-07-01 06:00:06
【问题描述】:

我正在尝试让 Rails 与 Digg API 的 OAuth 配合使用。我正在使用 oauth gem(红宝石,而不是导轨)。

我的代码大概是这样的:

@consumer = OAuth::Consumer.new(API_KEY, API_SECRET,
  :scheme => :header,
  :http_method => :post,
  :oauth_callback => "http://locahost:3000",
  :request_token_url => 'http://services.digg.com/1.0/endpoint?method=oauth.getRequestToken',
  :access_token_url => 'http://services.digg.com/1.0/endpoint?method=oauth.getAccessToken',
  :authorize_url => 'http://digg.com/oauth/authorize')
  @request_token = DiggController.consumer.get_request_token({
      :oauth_callback => "http://xx.xxx.xxx.x:3000/digg/callback"
    }, {
      'Content-Type' => 'application/x-www-form-urlencoded'
    })
session[:request_token] = @request_token.token
session[:request_token_secret] = @request_token.secret
redirect_to @request_token.authorize_url

就 gem 文档给我的内容而言,这是书本上的。但是,当调用 @consumer.get_request_token 时,Digg 向我吐出“400 Bad Request”错误。我无法弄清楚我做错了什么。有什么想法吗?


编辑:更新代码并添加 Wireshark 输出。我的错误现在是“需要 401 授权”。

Wireshark 的输出:

POST /1.0/endpoint?method=oauth.getRequestToken HTTP/1.1
Accept: */*
Connection: close
User-Agent: OAuth gem v0.3.6
Content-Type: application/x-www-form-urlencoded
Authorization: OAuth oauth_nonce="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  oauth_callback="http%3A%2F%2Fxx.xxx.xxx.x%3A3000%2Fdigg%2Fcallback",
  oauth_signature_method="HMAC-SHA1",
  oauth_timestamp="1268687137",
  oauth_consumer_key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  oauth_signature="xxx%2Bxxxxxxxxxxxxxxx%2Fxxxxxxx%3D", oauth_version="1.0"
Content-Length: 48
Host: services.digg.com

Content-Type=application%2fx-www-form-urlencoded



HTTP/1.1 401 Authorization Required
Date: Mon, 15 Mar 2010 21:05:37 GMT
Server: Apache
X-Powered-By: PHP/5.2.9-digg8
Cache-control: private
X-RateLimit-Current: 1
X-RateLimit-Max: 1000
X-RateLimit-Reset: 3600
X-Digg-Api-Version: 1.0
Accept-Ranges: bytes
Content-Length: 111
Keep-Alive: timeout=5, max=9998
Connection: Keep-Alive
Content-Type: text/xml;charset=utf-8

<?xml version="1.0" encoding="UTF-8"?>
<error code="5001" message="Invalid
signature" timestamp="1268687137"/>

【问题讨论】:

    标签: ruby-on-rails ruby oauth


    【解决方案1】:

    顺便说一下,回调参数不应该是localhost:3000,而是您的公共 IP 地址(确保还为您的计算机和/或路由器防火墙中的外部连接打开端口 3000),或者留给默认(带外)。

    检查引发的OAuth::Unauthorized 异常的内容(或使用嗅探器,例如tcpdumpWireshark)以获取有关HTTP 400 错误的更多详细信息(它们可能与您的某些参数有问题.)

    【讨论】:

    • 感谢您的提示,但我什至从未访问过 Digg 授权页面。目前唯一重要的参数是 request_token_url。
    • 使用 tcpdump 或 Wireshark 来查看 digg 在 HTTP 400 的正文中回复您的内容。顺便说一下,community.freshbooks.com/forums/viewtopic.php?pid=26769 有什么帮助吗?
    • 我在与 Twitter 和 MyspaceID 的 Oauth 连接中使用 localhost:3000 之类的回调。所以不能屏蔽
    • 仅仅因为“你使用它”并不意味着它是正确的。 RTFM @shingara, oauth.net/core/1.0a 谁说过“阻塞”?
    • 使用 Wireshark 我发现这是内容类型的问题,我相信我已经解决了这个问题,但现在收到“需要授权错误 401”,告诉我有一个“无效签名”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-01
    • 2014-11-30
    • 1970-01-01
    • 2017-12-15
    • 2015-08-25
    • 2013-07-11
    相关资源
    最近更新 更多