【问题标题】:Rhomobile: Not able to post the update on twitterRhomobile:无法在推特上发布更新
【发布时间】:2013-10-01 22:26:48
【问题描述】:

我正在使用 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 帐户获取,但响应也相同。

请帮我解决这个问题。我无法追踪我错过了什么或哪里出错了。

谢谢

【问题讨论】:

    标签: android twitter twitter-oauth rhomobile


    【解决方案1】:

    两件事:

    • status参数是普通的post数据参数,不应该在授权头,见https://dev.twitter.com/docs/api/1.1/post/statuses/update

    • API 1.1 存在一个奇怪的问题,即编码特殊字符(例如 %20 表示“”)。以下为我做了: url 为签名的输入编码“状态”的内容,而不是在帖子数据本身中对其进行编码。

    编辑:

    您忘记提供与 user_token 对应的密钥:

    $oauth_sign = get_auth_signature($post_status_url, @url_param, "")
    

    当您通过 https://api.twitter.com/oauth/access_token 从 Twitter 获得访问令牌时,Twitter 会回复 auth_token 和 auth_token_secret(以及 id 和 screen_name)。您在参数中正确提供了 auth_token(并因此提供给签名),但您应该在当前放置“”的位置提供 auth_token_secret。

    编辑 2:

    上面的编辑让我想知道:您是否使用 https://api.twitter.com/oauth/access_token 获得了访问令牌?因为为此,您应该使用 oauth_token_secret,它是从 twitter 调用提供给 https://api.twitter.com/oauth/request_token 的回调 url 中的参数。此回调 url 被称为对通过https://api.twitter.com/oauth/authorize 确认用户的响应。对于https://api.twitter.com/oauth/request_token,您可以使用 "" 作为机密,因为在该阶段还没有 auth_token(因此没有相应的机密)。

    【讨论】:

    • 感谢您的回复。实际上,我已经尝试过您提到的第一件事,但得到了相同的回复。
    • 我已经尝试了选项 2,因为您说只有签名输入的编码状态和在传递帖子正文时删除编码。仍然相同的错误消息“无法对您进行身份验证。”还有什么你想让我试一试或者我可能错过的任何东西吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2012-05-15
    • 2011-11-28
    相关资源
    最近更新 更多