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

2126 points
512d3f8f7df8cbb462001736_165830919
Submitted by
Nicholas Hazel
almost 4 years ago

Worked before, now it doesn't. Help?

I see no flaws with code. But it doesn't pass...

require 'rubygems'
require 'oauth'
require 'json'
baseurl = "https://api.twitter.com"
path    = "/1.1/statuses/user_timeline.json"
query   = URI.encode_www_form(
    "screen_name" => "twitterapi",
    "count" => 5,
)
address = URI("#{baseurl}#{path}?#{query}")
request = Net::HTTP::Get.new address.request_uri
def print_timeline(tweets)
 tweets.each{ |tweet|
   puts tweet['text']
 end
end
http             = Net::HTTP.new address.host, address.port
http.use_ssl     = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
consumer_key ||= OAuth::Consumer.new "ENTER IN EXERCISE 1", ""
access_token ||= OAuth::Token.new "ENTER IN EXERCISE 1", ""
request.oauth! http, consumer_key, access_token
http.start
response = http.request request
tweets = nil
if response.code == '200' then
  tweets = JSON.parse(response.body)
  print_timeline(tweets)
end
nil

1 vote

permalink

I found the trick was to go back to 1/5 and refresh the credentials by simply clicking "save & submit" than jumping back to 4/5. And also might want to change

"count" => 5,

to

"count" => 10,

other wise it will throw up another error.

CHEERS!

322 points
52f41753282ae3a6ee000241_156211101
Submitted by
Zachary
over 3 years ago


0 votes

permalink

Returns...

Oops, try again. The response code should be '200' but you received '401'. Double check the credentials you passed to the OAuth library. You may need to go back and execute your answer for exercise 1 in order to load the correct credentials. 

2126 points
512d3f8f7df8cbb462001736_165830919
Submitted by
Nicholas Hazel
almost 4 years ago

2 Comments

512d3f8f7df8cbb462001736_165830919 Nicholas Hazel almost 4 years ago

Yes, I have a twitter account and am familiar with the creds.

9e766e4880d9a79fd1244c1b04dc31fe?s=140&d=retro Nishanth over 3 years ago

Going back and resubmitting the Exercise 1 code helped. The Twitter API times out on your creds after a while.


0 votes

permalink

Newbie here, but what I figured out is that you have to allow certain permissions in the Twitter app. I changed the Settings to "Read and Write" and checked the box at the bottom to "Allow this application to be used to sign into Twitter."

Also, I noticed when I kept checking a few times, that it stopped working until I logged back into my account. Then it started working again.

This is just what I found through trial and error. Hope someone finds this helpful.

595 points
Ec7d5475bc43ab6b81dc1f68fecd042f?s=140&d=retro
Submitted by
Adrianne
almost 4 years ago

1 Comment

512d3f8f7df8cbb462001736_165830919 Nicholas Hazel almost 4 years ago

Yeah, I definitely passed based on passing full credentials to the API call (very bad idea). I suppose the jist of it is that you should understand the concept. I've goofed with enough APIs to know that they are liquid (ever-changing) and you simply need to read documentation, as the rate they evolve is much faster than some course or textbook could document. My advice is to test working implementations and view source code to see what's actually occurring.