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
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
Surprises in 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.
Free, biweekly scalability newsletter for SaaS CEOs
http://www.gregnavis.com/newsletter/
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
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 <