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

1473 points
4f10fd1a059e3b0001005036_405410413
Submitted by
Brad Milne
over 4 years ago

I'm getting the right information from the timeline but I keep getting an error

I've made the following changes: increase count to 10 in the query and then write a for loop to print out the text of the last 10 tweets. I keep getting the error:

You did not generate the appropriate number of output lines. Did you update the 'count' parameter?

query = URI.encodewwwform(
"screenname" => "twitterapi",
"count" => 10,
)
address = URI("#{baseurl}#{path}?#{query}")
request = Net::HTTP::Get.new address.request
uri

def print_timeline(tweets)
tweets.each do |tweet|
print tweet["text"]
end

end

Any ideas on what I'm doing wrong? Thanks!


6 votes

permalink

This one took me ages, so here's the whole code if you got stuck like me...

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

baseurl = "https://api.twitter.com"
path    = "/1.1/statuses/user_timeline.json"
query = URI.encodewwwform(
"screenname" => "twitterapi",
"count" => 10,
)
address = URI("#{baseurl}#{path}?#{query}")
request = Net::HTTP::Get.new address.requesturi

def print_timeline(tweets)
tweets.each do |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

Hope this helps :-)

1236 points
52ad96fe7c82ca447b008551_67114341
Submitted by
Laurence Marshall
over 4 years ago

1 Comment

Picture Juan P de Castro over 4 years ago

I tried modifying the code responsible for iterating the tweets and even the printing command print/puts, but nothing seemed to help. I copied your code, did need to modify it since there are some typos (i.e. URI.encodewwwform should be URI.encodewwwform) and ran it and it worked. I have no clue where the difference is but It works.


3 votes

permalink

Had the exact same problem. This worked for me, it's a slightly modified version of Laurence's code:
Copy source from here

540 points
Picture
Submitted by
Hugo Veeger
over 4 years ago

3 Comments

Picture Hugo Veeger over 4 years ago

problem is forum markup destroys code

Picture mikemp7 about 4 years ago

thanks

535c518c52f863c98b000ade_488145952 Dan Beneventano over 3 years ago

Thank you! Finally worked...


3 votes

permalink

adding another end
to the code on line 23 made mine work.

545 points
50fd89758b017ce0d0009f3b_591581881
Submitted by
Jake M
about 4 years ago


2 votes

permalink

OK figured out the issue. Instead of:

print tweet["text"]

I needed to put:

puts tweet["text"]

1473 points
4f10fd1a059e3b0001005036_405410413
Submitted by
Brad Milne
over 4 years ago


1 vote

permalink

I am having this problem and none of these two solutions work for me! I am new to Ruby!

1757 points
4ffa7c8aa76d59000300de00_223311523
Submitted by
eenvincible
over 4 years ago


1 vote

permalink

i'm unable to get mine working also... same error message about the incorrect count number

102 points
5187afcc4525104ab600438a_10403049
Submitted by
samuel harvey
over 4 years ago


0 votes

permalink

The following also gave me the same error. Apparently this exercise does not like blocks.

tweets.each{|t| puts t['text']}

1275 points
C3d709a8a4e893ec2372d86c580d84d2?s=140&d=retro
Submitted by
Randall McGrew
over 4 years ago


0 votes

permalink

Final code: Works ;)

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

baseurl = "https://api.twitter.com"
path    = "/1.1/statuses/user_timeline.json"
query = URI.encode_www_form(
"screenname" => "twitterapi",
"count" => 10,
)
address = URI("#{baseurl}#{path}?#{query}")
request = Net::HTTP::Get.new address.request_uri

def print_timeline(tweets)
tweets.each do |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

1148 points
59458bc92ddfa3180c000152_486959195
Submitted by
Ignacio Rodríguez
over 4 years ago


0 votes

permalink

If you are using print() instead of puts, make sure to separate each tweet by a new line.

def print_timeline(tweets)
tweets.each do |tweet|
print(tweet["text"])
print("\n")
end
end

215 points
D290b0646a5650bc01e923eb23350970?s=140&d=retro
Submitted by
u2pride
over 4 years ago


0 votes

permalink

Change line 28 from VERIFYPEER to VERIFYNONE fixed my issue with the certificate.

158 points
Picture
Submitted by
Michaela Glaze
almost 4 years ago


0 votes

permalink

Make sure you have at least 10 tweets posted on Twitter! ><

904 points
5910d920836966a6b1000163_504275576
Submitted by
Chris Bragg
over 3 years ago


0 votes

permalink

The difference was on line 11, place "screenname" => "twitterapi", instead of "screen_name" => "twitterapi"
The error was not because of puts or print methods.

254 points
Picture
Submitted by
Mia Petkovic
over 3 years ago