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

I am new to ruby want to create an api which post tweet to twitter
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’
‘Authorization: OAuth oauth_consumer_key=“i6baQCTt1sCXAo8YWcKhuly9Z”,
oauth_signature_method=“HMAC-SHA1”, oauth_timestamp=“1461568353”,
oauth_version=“1.0”’ --verbose

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

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

Please help me to get out of it

I think what you’re looking for is an HTTP client. You have plenty of

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

You might take a look at
Surprises in Ruby HTTP libraries by
Evans. She lists a dozen of gems.

Greg Navis
I help small tech companies to scale Heroku-hosted Rails apps.
Free, biweekly scalability newsletter for SaaS CEOs

Thanks for your reply ,

Ii tried with net/http but yes it work when i fetch tweets but did not
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”] =
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("
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

But It giving error of 220 your credential not allow.

If i change api and try to fetch tweets it works.

On Tue, Apr 26, 2016 at 7:57 PM, Paramnoor Singh <