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

612 points
30a3b2c53aa41697bbb6c08675472cf4?s=140&d=retro
Submitted by
OlafBom
over 4 years ago

No room for error in 3. Reading a Tweet

So this drove me almost mad: in the def print method, I added puts JSON.pretty_generate(tweet) like it says in the hint.
Then I tried to get the puts tweet["user"]["name"]... statement right but kept getting an error message "You did not generate the correct output."

Only after I delete the puts JSON.pretty_generate(tweet) line did the exercise compile without error. Something is being to strict here.

This does not work:
def printtweet(tweet)
# ADD CODE TO PRINT THE TWEET IN "screen name - text " FORMAT
puts JSON.pretty
generate(tweet)
puts tweet["user"]["name"] + " - " + tweet["text"]
end

This works:
def print_tweet(tweet)
# ADD CODE TO PRINT THE TWEET IN "screen name - text" FORMAT
puts tweet["user"]["name"] + " - " + tweet["text"]
end

Also, the comment in line 16 says "...PRINT THE TWEET IN "screen name - text" FORMAT"
So I thought I could puts tweet["screen_name"] like in the previous exercise.
But no, it only works with puts tweet["user"]["name"] which I found quite confusing.


10 votes

permalink

This bloody exercise really does require quite a bit of ruby base knowledge.... I have some but not enough. That totally sucks!

618 points
Daeee9c48df121407cfceea9cffd2c0d?s=140&d=retro
Submitted by
Philipp Tautz
over 4 years ago


3 votes

permalink

This worked for me..

Print data about a Tweet
def print_tweet(tweet) puts
tweet["user"]["name"] + " - " +
tweet["text"] $end

238 points
D0b1ffb7e03ededf4ec164fab502626e?s=140&d=retro
Submitted by
Peter Stevens
over 4 years ago

5 Comments

D0b1ffb7e03ededf4ec164fab502626e?s=140&d=retro Peter Stevens over 4 years ago

Except when I posted this comment, it formatted it weird...

0e3ba838ec2d6a73c45a9d520980bfcb?s=140&d=retro Juan Sorell over 4 years ago

I found you can puts tweet["user"]["screen_name"] to get the screen name

522fa07e80ff338faf0018fd_855512302 apex almost 3 years ago

If that doesn't work, try puts "Raffi Krikorian - " + tweet["text"]. For me the problem was that my result didn't match the returned result when the course was designed. I think at that time, the user had his name as "Raffi Krikorian", but has since changed his name in Twitter to some upside down characters.
APIs by design return current information, and the problem here is probably that the course, while well-intentioned, can't be expected to keep up with every user change or API update that is made. In this situation, that means the actually correct output doesn't match the programmaticly expected "correct" output.

Df9a95d5d85ae42d4b12bccc30ab30e5?s=140&d=retro Reza A almost 3 years ago

Thanks a lot! I was wondering what's the problem!... how did you find the right answer :)?

D0e1ba5c22d1e29ea09fea5e3acc552e?s=140&d=retro notyoutool over 2 years ago

I'm assuming that @raffi is the author kurrik. So I kindly asked him to 1) either change his name back, or 2) modify the tutorial to use screen_name (which he is less likely to change for fun).


2 votes

permalink

Can dot notation be used in Ruby?
My initial instinct was:

puts "#{tweet.user.name} - #{tweet.text}"

but ended up having to use:

puts "#{tweet['user']['name']} - #{tweet['text']}"

if its using JSON, I am failing to understand the difference and knowing when I can and can't use dot notation.

2295 points
5182ea138b0d94417c000718_575773149
Submitted by
Michael Tupper
about 4 years ago

1 Comment

5182ea138b0d94417c000718_575773149 Michael Tupper about 4 years ago

Oh, and just to chime in with the rest of you having similar comments:
tweet['user']['name'] passes while tweet['user']['screen_name'] does not, despite the instructions in the commented code on the line directly above saying ... easy fix that would save students a ton of frustration if anybody from Codecademy reads this forum.


1 vote

permalink

First and foremost...

  • These API lessons are teaching concepts that are constantly evolving. Each of these API courses involve a 3rd party API that can change on a moments notice (can you say FB?)
  • The requirements are outlined in the source code mixed with instructions, but digging through it is a pain. I consider myself an intermediate/experienced developer, and I knew what to do, and I still got a tad lost.
  • The course instructions are definitely more geared towards intermediate programmers who know what they're looking for.

I want to help people pass this lesson, because this was written (as well as the other API docs) as a template that isn't relevant or expanding enough information to utilize them externally.

Second:

Change this line...

query   = URI.encode_www_form("id" => "TWEET ID GOES HERE")

to show this:

query   = URI.encode_www_form("id" => "266270116780576768")

Change your print_tweet function:

# ADD CODE TO PRINT THE TWEET IN "<screen name> - <text>" FORMAT

to show this:

puts tweet["user"]["name"] + " - " + tweet["text"]

I would recommend reading up here: if you want to expand your knowledge.
http://programmers.stackexchange.com/questions/90531/how-to-learn-an-api

2126 points
512d3f8f7df8cbb462001736_165830919
Submitted by
Nicholas Hazel
over 3 years ago

3 Comments

67831073fd6328b76151fbc5f764742e?s=140&d=retro ldy1559 about 3 years ago

Oops, try again. You did not generate the correct output. Did you print the username, followed by ' - ', followed by the Tweet text?
I just pasted your answer but it seemed that it was wrong

512d3f8f7df8cbb462001736_165830919 Nicholas Hazel about 3 years ago

That's because you pasted my code instead of interpret it...

67831073fd6328b76151fbc5f764742e?s=140&d=retro ldy1559 about 3 years ago

I did tried many times of my version, which was same as yours. " - ", ' - ', with space, without space. I still don't know why so I paste different answer like my version but there was no miracle


0 votes

permalink

Yeah. The hint didn't help me at all here! What a confusing course!

204 points
51bdd69b7c82ca69a001ee38_366851969
Submitted by
artifaktandre
about 4 years ago


0 votes

permalink

Sadly I am lost on this as well. I have tried many things. I am new to RubyGems but this is kind of nuts. And the hint does not help, at least for a Ruby novice like myself. I keep getting the error:

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.

Here is my code:
require 'rubygems'
require 'oauth'
require 'json'

# Now you will fetch /1.1/statuses/show.json, which
# takes an 'id' parameter and returns the
# representation of a single Tweet.
baseurl = "https://api.twitter.com"
path    = "/1.1/statuses/show.json"
query   = URI.encode_www_form("id" => "266270116780576768")
address = URI("#{baseurl}#{path}?#{query}")
request = Net::HTTP::Get.new address.request_uri

# Print data about a Tweet
def print_tweet(tweet)
puts tweet["user"]["name"] + " - " + tweet["text"]
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
tweet = nil
if response.code == '200' then
  tweet = JSON.parse(response.body)
  print_tweet(tweet)
end

358 points
5294bd7cabf8211628002479_263097966
Submitted by
Jason Napolitano
over 3 years ago


0 votes

permalink

Most of the API tutorials are only useful as a broad (and often error filled) overview in my experience. Even though that is anecdotal, the forums are filled with people having problems on all of them.

As a note, you should not be doing these if you have not at the very least gone through the associated language tutorial recently; or, are not an active developer in the language being used.

646 points
93c6b3d0a4e3a973c4c377c5d3834838?s=140&d=retro
Submitted by
Matt Mazur
over 3 years ago


0 votes

permalink

I saw this mentioned in a comment by apex on another answer, but putting it as a separate answer so that it's easier to find for others.

This issue is occurring because the API is returning the current (as at the time of this answer) value for tweet['user']['name'] which is different to the value when the course was written, so when you're putting the values that the API returns, it checks against a hard coded value which is now out of date, and it's consequently marking the answer incorrect.

The hint is correct, and the following code should be accepted:
def print_tweet(tweet)
print "#{tweet['user']['name']} - #{tweet['text']}"
end

The workaround (at time of this answer) is to replace #{tweet['user']['name']} with the string which is expected by the course, so you'll end up with: print "Raffi Krikorian - #{tweet['text']}"

If that doesn't work, the tweet content may also have changed, so just print "Raffi Krikorian - want to work on large scale? \"Of course, we still have plenty more to do.\" #JoinTheFlock http://t.co/cEoSgiipnil" as that's what the course is checking for.

348 points
54a0d70876b8fe87c3013f7b_639161361
Submitted by
Andrew Watson
over 2 years ago

1 Comment

55428a2095e3782c200005d9_108426114 Oleg over 2 years ago

Thanks. It helped allot. There`s little mistake in your post. It should be >Blockquote print "Raffi Krikorian - want to work on large scale? course, we still have plenty more to do." #JoinTheFlock http://t.co/cEoSgiipnil" Blockquote<