我正在使用 Rhomobile(适用于 Android)在 Twitter 上发布状态更新。为此,按照 Oauth 实现的步骤,我可以使用 Twitter 登录,但登录后,每次我收到 HTTP 响应时尝试发布状态更新为{"errors":[{"message":"Could not authenticate you","code":32}]}
.
以下是请求发布状态更新的相关代码。
def post_to_twitter(comment)
$rnd = rand(36**32).to_s(36)
$post_status_url = "https://api.twitter.com/1.1/statuses/update.json"
$oauth_token1 = @account_set.tt_oauth_token
$oauth_token_secret1 = @account_set.tt_oauth_token_secret
@oauth_nonce = $rnd
@oauth_timestamp = Time.now.to_i.to_s
@http_port = System.get_property('rhodes_port')
@url_param = "oauth_consumer_key="+ $oauth_consumer_key + "&" +
"oauth_nonce=" + @oauth_nonce + "&" +
"oauth_signature_method=" + $oauth_signature_method + "&" +
"oauth_timestamp=" + @oauth_timestamp + "&" +
"oauth_token=" + $oauth_token1 + "&" +
"oauth_version="+ $oauth_version + "&" +
"status=" + Rho::RhoSupport.url_encode("Test")
$oauth_sign = get_auth_signature($post_status_url, @url_param, "")
@auth_header = "OAuth oauth_consumer_key="+ $oauth_consumer_key + ", " +
"oauth_nonce=" + @oauth_nonce + ", " +
"oauth_signature=" + $oauth_sign + ", " +
"oauth_signature_method=" + $oauth_signature_method + ", " +
"oauth_timestamp=" + @oauth_timestamp + ", " +
"oauth_token=" + $oauth_token1 + ", " +
"oauth_version="+ $oauth_version + ", " +
"status=" + Rho::RhoSupport.url_encode("Test")
postTTres = Rho::AsyncHttp.post(
:url => $post_status_url,
:headers =>{"Content-Type" => "application/x-www-form-urlencoded", "Authorization" => @auth_header }
)
p postTTres
end
签名生成函数如下:
def get_auth_signature (url, url_param, secret)
signature = "POST&" + Rho::RhoSupport.url_encode(url).to_s +
"&" + Rho::RhoSupport.url_encode(url_param).to_s
key = $oauth_consumer_secret + "&" + secret
hmac = HMAC::SHA1.new(key)
hmac.update(signature)
$signature = Base64.encode64("#{hmac.digest}")
$signature = Rho::RhoSupport.url_encode("#{$signature.gsub(/\n/,'')}")
return $signature
end
跟踪时的参数值如下:
生成签名前的@url_param ---------
oauth_consumer_key=2ChmEzWBe5Y9hMYODqA1IQ&oauth_non ce=iyb9502vspjnhj9orb87sriych16678b&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1380117614&oauth_token=244586214-M6A2jMlR7vZiqwAMrfuSj7I7XFzFTRd4 6nV6aTLK&oauth_version=1.0&status=Test`
传递@url_param
给get_auth_signature()
生成签名。生成的签名url和签名是
签名字符串 ----------
POST&https%3A%2F%2Fapi.twitter.com%2F1.1%2Fstatuses%2Fupdate。json&oauth_consumer_key%3D2ChmEzWBe5Y9hMYODqA1IQ%26oauth_nonce%3Diyb9502vspjnhj9orb87sriych16678b%26oauth_signature_method%3DHMAC-SHA1%26oauth_timesta mp%3D1380117614%26oauth_token%3D244586214-M6A2jMlR7vZiqwAMrfuSj7I7XFzFTRd46nV6aTLK%26oauth_version%3D1.0%26status%3DTest"`
Base64 字符串 --------
gjdXuU3qoGNt90Q2dRhNM3IXaBI%3D
将所有这些值作为标头授权传递给https://api.twitter.com/1.1/statuses/update.json
并得到
Http 响应为{"errors":[{"message":"Could not authenticate you","code":32}]}
.
还尝试将其作为 post 参数传递给 :body 但没有运气。
postTTres = Rho::AsyncHttp.post(
:url => $post_status_url,
:headers =>{"Content-Type" => "application/x-www-form-urlencoded"},
:body => @url_param + "&oauth_signature=" + $oauth_sign
)
使用 Twitter 服务器计时检查系统计时,这很好。
还尝试了静态 oauth_token ,我们可以从 Twitter 帐户获得,但响应也相同。
请帮我解决这个问题。我无法追踪我错过了什么或哪里出错了。
谢谢