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

1334 points
15ce11231535bd0e1388d65b6f2156f0?s=140&d=retro
Submitted by
Trumble
almost 4 years ago

I think I'm doing this exercise correctly, but the API is returning a change.

My code which I think is right:

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

# Now you will fetch /1.1/statuses/user_timeline.json,
# returns a list of public Tweets from the specified
# account.
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

# Print data about a list of Tweets
def print_timeline(tweets)
  # ADD CODE TO ITERATE THROUGH EACH TWEET AND PRINT ITS TEXT
    tweets.each do |tweet|
        count = 0
        if count < 10
            puts tweet["text"]
            count += 1
        end
    end
end

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

# If you entered your credentials in the first
# exercise, no need to enter them again here. The
# ||= operator will only assign these values if
# they are not already set.
consumer_key ||= OAuth::Consumer.new "ENTER IN EXERCISE 1", ""
access_token ||= OAuth::Token.new "ENTER IN EXERCISE 1", ""

# Issue the request.
request.oauth! http, consumer_key, access_token
http.start
response = http.request request

# Parse and print the Tweet if the response code was 200
tweets = nil
if response.code == '200' then
  tweets = JSON.parse(response.body)
  print_timeline(tweets)
end
nil

Returning:

"If a user has granted your app direct message read access, OAuth can be used to display any uploaded media: https://t.co/BIjdJ8J637
Important: On Jan 14, 2014 all requests to http://t.co/JJ1WIaJkjK will require SSL & disallow plaintext connections. https://t.co/5bffaNC1i5
Developers in Brazil can keep abreast of @twitterapi news by following @TwitterDevBr, our new Portuguese account managed by @lfcipriani!
We upgraded the SSL certificate on http://t.co/JJ1WIaJkjK today as planned. More info on this G3 certificate here: https://t.co/V4K1qHVZe9
Unicode spaces (not only ASCII spaces) are now word separators while tracking keywords with statuses/filter. Try it: https://t.co/hUMpNCWmyi
nil"

I am guessing the API has changed, so this lesson needs updating?


2 votes

permalink

What's returned there are the tweet texts. The problem is you're only doing it 5 times, because query has a count of 5, that means the each walks out of the tweet list after those 5 queries, so the if is pointless.

1618 points
Picture
Submitted by
Devieus
almost 4 years ago

2 Comments

Picture Mark Omwansa almost 4 years ago

I change the count to 10 and it worked. Thanks for the help

15ce11231535bd0e1388d65b6f2156f0?s=140&d=retro Trumble almost 4 years ago

Oh I thought I replied to this, Thanks for the help Devieus on this one. I just completely missed the line myself, maybe too long looking at it!


2 votes

permalink

We can clean up your code quite a bit.

query   = URI.encode_www_form(
    "screen_name" => "twitterapi",
    "count" => 5,
)

It asks you to change the count here. This is simpler than they way you did it. It should be

query   = URI.encode_www_form(
    "screen_name" => "twitterapi",
    "count" => 10,
)

Which means when you iterate over the tweets, all you have to do is this:

# Print data about a list of Tweets
def print_timeline(tweets)
  # ADD CODE TO ITERATE THROUGH EACH TWEET AND PRINT ITS TEXT
    tweets.each do |tweet|
      puts tweet["text"]
    end
end

Hope this helps!

794 points
Picture
Submitted by
Michael Ciocca
almost 4 years ago

1 Comment

15ce11231535bd0e1388d65b6f2156f0?s=140&d=retro Trumble almost 4 years ago

Hi Michael,

Thanks for responding I did make these changes after I realised the problems a few days after the question, but thank you for taking the time to respond!