Ruby on Rails Tuesday, April 26, 2016

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


On Mon, Apr 25, 2016 at 6:37 PM, Greg Navis <contact@gregnavis.com> wrote:
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.

--
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-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@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.

--
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-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAJMzsO9okWzSXw%3DShAeW7iqzhSXxSu0aKe2JEZddiH9wmi%3DufA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment