Rhomobile: Not able to post the update on twitter


#1

Rhomobile: Not able to post the update on twitter

I am using Rhomobile (for Android) to post status update on Twitter. For that, following the steps of Oauth implementation I am able to get logged in with Twitter but after login, when trying to post the status update everytime I got HTTP Response as {“errors”:[{“message”:“Could not authenticate you”,“code”:32}]}.

Below is the relevant code for making the request to post status update.

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 }
#      :body => @url_param + "&oauth_signature=" + $oauth_sign
    )

	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

The parameters values when traced are as follows:
@url_param before generating signature --------- 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

Passing @url_param to get_auth_signature() to generate signature.
Generated signature urla nd signature is

Signature string ---------- 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 string ------- gjdXuU3qoGNt90Q2dRhNM3IXaBI%3D

Passing all these values as header Authorization to https://api.twitter.com/1.1/statuses/update.json
and got
Http Response as {“errors”:[{“message”:“Could not authenticate you”,“code”:32}]}.
Also tried passing it as post parameters in :body but no luck.
Checked the system timings with Twitter server timings and thats fine.
I have also tried with static oauth_token that we can get from Twitter account but then too same response.

Please help me to fix this. I am unable to trace what I am missing or where I am going wrong.

Thanks


#2

I think you’re sending multiple authentication challenges – both your POST body and the HTTP authorization header contain OAuth attributes. I would send the OAuth authorization only in the HTTP header, and leave your POST body to just the parameters concerned with the method at hand, statuses/update.

Your signature generation code itself appears correct at first glance.


#3

Hey Taylor, Thanks for reply.

I have tried with sending OAuth authorization only in the HTTP header and status (tweet text) in POST body.

So, now its like this

@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
$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
              
postTTres = Rho::AsyncHttp.post(
  :url => $post_status_url,
  :headers =>{'Content-Type' => 'application/x-www-form-urlencoded', 'Authorization' => @auth_header },
  :body => {'status' => Rho::RhoSupport.url_encode("Test")}
)   

Still getting same error message ‘Could not authenticate you’.
What could be the issue with this now? Seems anything missing or wrong with this ?

Thanks