This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Introduction to Twitter's API Forum

View Course » View Exercise

1240 points
7127165e878d32591eab52f6409d88ca?s=140&d=retro
Submitted by
Hannah Cobb
about 3 years ago

I cannot get past 5.5

require 'rubygems'
require 'oauth'
require 'json'

You will need to set your application type to

read/write on dev.twitter.com and regenerate your access

token. Enter the new values here:

consumerkey = OAuth::Consumer.new(
"READ/WRITE CONSUMER KEY = XJlzIpC2W3e4Uoar1vXAh1EbD",
"READ/WRITE CONSUMER SECRET = VLvmdVrEWQz2pbGCawj6TtdguxVeB2skrZvLjZ6LDplEpeziXQ ")
access
token = OAuth::Token.new(
"READ/WRITE ACCESS TOKEN = https://api.twitter.com/oauth/access_token",
"READ/WRITE ACCESS SECRET = 4ayl0OEdn23XShH9AlsXKhT5B")

Note that the type of request has changed to POST.

The request parameters have also moved to the body

of the request instead of being put in the URL.

baseurl = "https://api.twitter.com"
path = "/1.1/statuses/update.json"
address = URI("#{baseurl}#{path}")
request = Net::HTTP::Post.new address.requesturi
request.set
form_data(
"status" => "Saturday",
)

Set up HTTP.

http = Net::HTTP.new address.host, address.port
http.usessl = true
http.verify
mode = OpenSSL::SSL::VERIFY_PEER

Issue the request.

request.oauth! http, consumerkey, accesstoken
http.start
response = http.request request

Parse and print the Tweet if the response code was 200

tweet = nil
if response.code == '200' then
tweet = JSON.parse(response.body)
puts "Successfully sent #{tweet["text"]}"
else
puts "Could not send the Tweet! " +
"Code:#{response.code} Body:#{response.body}"
end

The error I get is:Could not send the Tweet! Code:400 Body:{"errors":[{"message":"Bad Authentication data","code":215}]}
nil


0 votes

permalink

read my code to identify your mistake
require 'rubygems'
require 'oauth'
require 'json'


consumer_key ||= OAuth::Consumer.new("cRPQUFuKBvvWKCCIuawJvIy7h","eG2LsdIuBa2o53QADUAVfsz2IPA4X150hihWIx4I5wIxfB7SAz")
access_token  ||= OAuth::Token.new("841368199-gx50VJVJSgnHVw8ScGmEWtgEy6gtLGJf0a5j70oN", "5zrzFU9d9z2Vt9RvWvU40TNN0EM7DypgPXhz5O4Es6sj1")


baseurl = "https://api.twitter.com"
path    = "/1.1/statuses/update.json"
address = URI("#{baseurl}#{path}")
request = Net::HTTP::Post.new address.request_uri
request.set_form_data(
  "status" => "Test",
)

http             = Net::HTTP.new address.host, address.port
http.use_ssl     = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER

request.oauth! http, consumer_key, access_token
http.start
response = http.request request

tweet = nil
if response.code == '200' then
  tweet = JSON.parse(response.body)
  puts "Successfully sent #{tweet["text"]}"
else
  puts "Could not send the Tweet! " +
  "Code:#{response.code} Body:#{response.body}"
end

1582 points
556d33ab937676099b000222_360659495
Submitted by
buu thong tran
about 3 years ago


0 votes

permalink

you may also need to set your app to read and write mode by going to map management -> Permissions -> Access then check the button ''read and write''

1582 points
556d33ab937676099b000222_360659495
Submitted by
buu thong tran
about 3 years ago