Want to write api of twitter to post tweet without any gem/library means with curl command

32 views
Skip to first unread message

Paramnoor Singh

unread,
Apr 25, 2016, 3:30:22 AM4/25/16
to rubyonra...@googlegroups.com
Hello
I am new to ruby want to create an api which post tweet to twitter without help of any gem include as we did on command line by running below code:

curl --request 'POST' 'https://api.twitter.com/1.1/statuses/update.json' --data 'status=Maybe+he%27ll+finally+find+his+keys.+%23peterfalk' --header 'Authorization: OAuth oauth_consumer_key="i6baQCTt1sCXAo8YWcKhuly9Z", oauth_nonce="12f94f3e4b2ded3bbfdfe781de60ae73", oauth_signature="x6%2Fi2w%2F0RjN4prcFmA5HthOZU3Q%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1461568353", oauth_token="2291131736-LpQkfe5diMTung5mVQ0Dc5EKA9u8qnIgWuPqau9", oauth_version="1.0"' --verbose


But in controller how can we use this command so it get ouath_signature and post the tweet.

As In php it did with curl want same way ion ruby.

Please help me to get out of it

Greg Navis

unread,
Apr 25, 2016, 9:08:06 AM4/25/16
to rubyonra...@googlegroups.com
I think what you're looking for is an HTTP client. You have plenty of options:

* curl binding for Ruby
* Net::HTTP in the standard library
* plenty of gems built on top of those

You might take a look at http://jvns.ca/blog/2016/03/04/whats-up-with-ruby-http-libraries/ by Julia Evans. She lists a dozen of gems.
--
Greg Navis
I help small tech companies to scale Heroku-hosted Rails apps.

Paramnoor Singh

unread,
Apr 26, 2016, 10:27:32 AM4/26/16
to rubyonra...@googlegroups.com
Thanks  for your reply ,

Ii tried with net/http but yes it work when i fetch tweets but did not work when i post a tweet. Here below is my code for that.


require "base64"
require "json"
require "net/http"
require "net/https"
require "uri"

### Setup access credentials

consumer_key = "XXXXXXXXX"
consumer_secret = "XXXXXXXXX"

### Get the Access Token

bearer_token = "#{consumer_key}:#{consumer_secret}"
bearer_token_64 = Base64.strict_encode64(bearer_token)

token_uri = URI("https://api.twitter.com/oauth2/token")
token_https = Net::HTTP.new(token_uri.host,token_uri.port)
token_https.use_ssl = true

token_request = Net::HTTP::Post.new(token_uri)
token_request["Content-Type"] = "application/x-www-form-urlencoded;charset=UTF-8"
token_request["Authorization"] = "Basic #{bearer_token_64}"
token_request.body = "grant_type=client_credentials"

token_response = token_https.request(token_request).body
#@timeline_json = token_response

token_json = JSON.parse(token_response)

access_token = token_json["access_token"]
#puts access_token
#@timeline_json = access_token
### Use the Access Token to make an API request


timeline_uri = URI("https://api.twitter.com/1.1/statuses/update.json?screen_name=testingclient1")
timeline_https = Net::HTTP.new(timeline_uri.host,timeline_uri.port)
timeline_https.use_ssl = true

timeline_request = Net::HTTP::Post.new(timeline_uri)
#@timeline_json = timeline_request
timeline_request["Authorization"] = "Bearer #{access_token}"
timeline_request["Content-Type"] = "application/json;charset=UTF-8"
timeline_request["status"] = "my tweet"

timeline_response = timeline_https.request(timeline_request).body
timeline_json = JSON.parse(timeline_response)

puts JSON.pretty_generate(timeline_json)
@timeline_json = timeline_json


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAA6WWt_PYw_UGgu7NVNAADc0YZDPGrfPX5DiHev2c3DXzdFrSQ%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

Paramnoor Singh

unread,
Apr 26, 2016, 10:28:21 AM4/26/16
to rubyonra...@googlegroups.com
But It giving error of 220 your credential not allow.

If i change api and try to fetch tweets it works.
Reply all
Reply to author
Forward
0 new messages