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

884 points
52a5c9df548c355247001428_819369585
Submitted by
anonymous
about 3 years ago

Need help 5/5

here is my code:
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",
"READ/WRITE CONSUMER SECRET",
"READ/WRITE dev.twitter.com")
access
token = OAuth::Token.new(
"READ/WRITE ACCESS TOKEN",
"READ/WRITE ACCESS SECRET",
"READ/WRITE dev.twitter.com")

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" => "Testing 1.2.3",
)

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

and ive tried everything even what other people told me to do nothing seemed to work heres the error i get with this code:

Oops, try again. The response code should be '200' but you received '401'. Double check the credentials you passed to the OAuth library. Make sure that you changed your application to 'Read and Write' and copied the NEW values for your consumer and access tokens.

please help me...


2 votes

permalink

You should be setting read/write on dev.twitter.com, not in your code. After you adjust that setting, you'll need to regenerate your keys and copy the new values into your code.

1202 points
536c6b768c1ccc23970005cb_819363073
Submitted by
jiltob
about 3 years ago

1 Comment

52a5c9df548c355247001428_819369585 anonymous about 3 years ago

thanks :D