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

969 points
51a95c1ca9174cacb900006a_617271793
Submitted by
André Hess B
over 4 years ago

Works on codecademy, doesnt work on my computer

I don't understand. I have successfully passed step #1 which returns a 200 status code. However if I paste the same exact code and run it on my computer it returns a 401 code.

Why could this be happening?

Thanks in advance

Here's the code i'm using:

require 'rubygems'
require 'oauth'

# Change the following values to those provided on dev.twitter.com
# The consumer key identifies the application making the request.
# The access token identifies the user making the request.
consumer_key = OAuth::Consumer.new(
    "....",
    "....")
access_token = OAuth::Token.new(
    "....",
    "....")

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

# The verify credentials endpoint returns a 200 status if
# the request is signed correctly.
address = URI("#{baseurl}/1.1/account/verify_credentials.json")

# Set up Net::HTTP to use SSL, which is required by Twitter.
http = Net::HTTP.new address.host, address.port
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER

# Build the request and authorize it with OAuth.
request = Net::HTTP::Get.new address.request_uri
request.oauth! http, consumer_key, access_token

# Issue the request and return the response.
http.start
response = http.request request
puts "The response status was #{response.code}"

0 votes

permalink

Hi Andre,

Could you paste the code? (without your keys!)

Chloe

241 points
C980e95388fefa946d4c63b5b2a761bb?s=140&d=retro
Submitted by
chloe88
over 4 years ago

4 Comments

51a95c1ca9174cacb900006a_617271793 André Hess B over 4 years ago

HI Chloe, just edited my response with the code, replacing the keys with .... Thanks in advance

C980e95388fefa946d4c63b5b2a761bb?s=140&d=retro chloe88 over 4 years ago

Hi Andre,

The code looks fine from first inspection, do you have the 'oauth' gem installed?

Chloe

C980e95388fefa946d4c63b5b2a761bb?s=140&d=retro chloe88 over 4 years ago

Actually on second thoughts I guess you do if you're getting a 401 error.

https://dev.twitter.com/docs/error-codes-responses
The twitter docs says your credentials were incorrect. Maybe regenerate your tokens and try again?

6c820172d5992c4857056ce78ab72e51?s=140&d=retro heshamelmahdy.info about 3 years ago

I have the same problem.. any clue?