RoR Twitter API oAuth : "Could not authenticate you."

I try to connect my app to the Twitter API. I am not sure but i think my problem come from the oauth_signature. So i read this documentation : https://dev.twitter.com/oauth/overview/creating-signatures

I think i did everything like they say but it's doesn't work and i don't have any clues why.

Here is my code :

  def build_url_api(parameters, token, account, secret_token)
   consumer_key = 'xxx'
   secret_consumer_key = 'xxx'

   oauth_consumer_key = consumer_key
   oauth_signature_method = "HMAC-SHA1"
   oauth_timestamp = Time.zone.now.to_i.to_s
   oauth_nonce = Digest::SHA256.hexdigest(oauth_timestamp)[0..16]
   oauth_version = "1.0"
   oauth_token = token
   url = 'https://api.twitter.com/1.1/statuses/user_timeline.json'

   parameters = 'oauth_consumer_key=' +
   oauth_consumer_key +
   '&oauth_nonce=' +
   oauth_nonce +
   '&oauth_signature_method=' +
   oauth_signature_method +
   '&oauth_timestamp=' +
   oauth_timestamp +
   '&oauth_token=' +
   oauth_token +
   '&oauth_version=' +
   oauth_version

   base_string = 'GET&' + CGI.escape(url) + '&' + CGI.escape(parameters)
   secret_key = secret_consumer_key + '&' +secret_token
   oauth_signature = CGI.escape(Base64.encode64("#{OpenSSL::HMAC.digest('sha1',secret_key, base_string)}").chomp)
   url = url + '?' + parameters + '&oauth_signature=' + oauth_signature
 end

OAuth parameters have names like oauth_* and are different from URL parameters. You need to send them in Authorization HTTP header, not as query string in URL. The format of authorization header's value is like: OAuth pe_name1="pe_value1", pe_name2="pe_value2",… pe_name_last="pe_value_last" . Here pe_ means percent encoded.

Also note that, in general, you need to use actual URL params also, to generate base string. Otherwise it will produce wrong signature for requests with URL parameters (like count=39 ).


Shame on me i found the solution :

I just had an old consumer_key.

My code sample here works well.

链接地址: http://www.djcxy.com/p/66194.html

上一篇: 点子找到与搜索包,但不会安装它

下一篇: RoR Twitter API oAuth:“无法验证你的身份。”