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

1658 points
Picture
Submitted by
Steel Brain
almost 4 years ago

Answer: Parsing a User Object

require 'rubygems'
require 'oauth'
require 'json'
# Parse a response from the API and return a user object.
def parse_user_response(response)
  user = nil

  # Check for a successful request
  if response.code == '200'
    # Parse the response body, which is in JSON format.
    # ADD CODE TO PARSE THE RESPONSE BODY HERE
    user = JSON.parse(response.body)

    # Pretty-print the user object to see what data is available.
    puts "Hello, #{user["screen_name"]}!"
  else
    # There was an error issuing the request.
    puts "Expected a response of 200 but got #{response.code} instead"
  end

  user
end

# All requests will be sent to this server.
baseurl = "https://api.twitter.com"

# Verify credentials returns the current user in the body of the response.
address = URI("#{baseurl}/1.1/account/verify_credentials.json")

# 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 previous
# 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 = Net::HTTP::Get.new address.request_uri
request.oauth! http, consumer_key, access_token
http.start
response = http.request(request)
user = parse_user_response(response)

7 votes

permalink

Thanks for this! I was stuck for a while; I didn't know what to do.

1152 points
50fc8ccdea6e824b6a002aa2_265157911
Submitted by
Jobsamuel Núñez
over 3 years ago

4 Comments

Picture Mark Omwansa over 3 years ago

Ditto

5325ccd3282ae31e61000721_225905004 aharrison0013 over 3 years ago

This did not work! I got this 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.

Can someone further help me?

5033db2abb95d2000200a2d4_696552887 amirahaile over 2 years ago

@aharrison0013 Do exactly what it told you. Go back to exercise 1 and re-enter your credentials (the API keys and Tokens).

Picture Leo M. over 2 years ago

It worked when I removed the line "require 'json'" @aharrison0013


2 votes

permalink

Worked for me too. Thanks!

48 points
Picture
Submitted by
JohnRLive
over 3 years ago


1 vote

permalink

This didn't really work for me. It allows me to pass to the next lesson but displays this:
SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed

574 points
Picture
Submitted by
Miguel Fondeur
over 3 years ago


1 vote

permalink

doesn't work

1853 points
54ac33ccd3292f3cd6002219_316521852
Submitted by
sotiris
over 2 years ago