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

985 points
0868c1b0f685bee750fe792951033742?s=140&d=retro
Submitted by
Cory
about 4 years ago

Getting the right result, but showing an error

Here is the first half of my code. It results in NAME - TWEET, but returns an error. I'm guessing the issue is using the +, but I don't know exactly what it is looking for. Any ideas?

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

baseurl = "https://api.twitter.com"
path = "/1.1/statuses/show.json"
query = URI.encodewwwform("id" => "266270116780576768")
address = URI("#{baseurl}#{path}?#{query}")
request = Net::HTTP::Get.new address.request_uri

def printtweet(tweet)
puts JSON.pretty
generate(tweet)
tweet["user"]["name"] + ' - ' + tweet["text"]
end


3 votes

permalink

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

baseurl = "https://api.twitter.com"
path = "/1.1/statuses/show.json"
query = URI.encodewwwform("id" => "266270116780576768")
address = URI("#{baseurl}#{path}?#{query}")
request = Net::HTTP::Get.new address.request_uri

def print_tweet(tweet)

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

http = Net::HTTP.new address.host, address.port
http.usessl = true
http.verify
mode = OpenSSL::SSL::VERIFY_PEER

consumerkey ||= OAuth::Consumer.new "ENTER IN EXERCISE 1", ""
access
token ||= OAuth::Token.new "ENTER IN EXERCISE 1", ""

request.oauth! http, consumerkey, accesstoken
http.start
response = http.request request

tweet = nil
if response.code == '200' then
tweet = JSON.parse(response.body)
print_tweet(tweet)
end

1333 points
51245087cc380b4567000b63_511518481
Submitted by
Patrick Beatty
about 4 years ago

6 Comments

51245087cc380b4567000b63_511518481 Patrick Beatty about 4 years ago

Again, removed the commenting from the code because when put in answer form it just makes the font very large.

52ad96fe7c82ca447b008551_67114341 Laurence Marshall about 4 years ago

Thank you. That worked. :-)

51245087cc380b4567000b63_511518481 Patrick Beatty about 4 years ago

Happy to help :)

52ad96fe7c82ca447b008551_67114341 Laurence Marshall about 4 years ago

:-)

4f0b2b9085a9d7000101d413_711447490 Max West about 4 years ago

My code block looks like this:

Print data about a Tweet

def printtweet(tweet)
# ADD CODE TO PRINT THE TWEET IN " - " FORMAT
puts JSON.pretty
generate(tweet)
puts tweet["user"]["name"] + '-' + ["text"]

I get:
can't convert Array into String
Oops, try again.
You did not generate the correct output. Did you print the username, followed by ' - ', followed by the Tweet text?

Since nothing is actually taught I have no way of knowing what response to give. The answer above does not work.
end

513c003ea6f1d4517a0046d1_809760095 Mike H about 4 years ago

If anyone still has trouble after following this, you have to enter in your Consumer Keys and Access Tokens down below. Just copy them from Exercise 1 and Paste. Four codes in total. Ignore comments that say you don't need to enter them. It was the missing link for me before being able to finally pass. What a headache this Exercise was.


1 vote

permalink

You're not "puts"ing the output to the screen.

You use JSON.pretty...to see the output of a whole tweet object. You don't need to output it for the actual answer.

Then "puts tweet["user"]["name"] + ' - ' + tweet["text"]" should output in the correct format.

Did that make sense?

1031 points
Picture
Submitted by
James White
about 4 years ago

3 Comments

0868c1b0f685bee750fe792951033742?s=140&d=retro Cory about 4 years ago

Yes, that is the solution. Thanks a lot!

Picture James White about 4 years ago

Quite welcome. :)

52ad96fe7c82ca447b008551_67114341 Laurence Marshall about 4 years ago

Sorry, but I'm not sure on what you mean, please could you expand, i.e. write out the code. Thanks :-)


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


0 votes

permalink

Try
puts tweet["user"]["screen_name"] + '-' + ["text"]

280 points
C2dd2d396359de710fa78622094fe975?s=140&d=retro
Submitted by
Cian Han
over 3 years ago


-2 votes

permalink

I enter this:

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.encodewwwform("id" => "266270116780576768")
address = URI("#{baseurl}#{path}?#{query}")
request = Net::HTTP::Get.new address.request_uri

Print data about a Tweet

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

Set up HTTP.

http = Net::HTTP.new address.host, address.port
http.usessl = 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.

consumerkey ||= OAuth::Consumer.new "ENTER IN EXERCISE 1", ""
access
token ||= OAuth::Token.new "ENTER IN EXERCISE 1", ""

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)
print_tweet(tweet)
end

and I get this:

can't convert String into Integer
Oops, try again.
You did not generate the correct output. Did you print the username, followed by ' - ', followed by the Tweet text?

888 points
4f0b2b9085a9d7000101d413_711447490
Submitted by
Max West
about 4 years ago