This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Introduction to Twitter's API Forum

View Course

976 points
53c92c81282ae38b530013b9_831050019
Submitted by
kurt.d.w.89
almost 3 years ago

1.5. My response code is 401 but should be 200. Could somebody look at my code and help me?

Hi everybody, I am getting the "response code should be '200' but you received '401'." roadblock here, and I honestly am not sure what I am doing wrong. I have below all of my code. Is there anyone that could take a look at it for me and tell me what I am missing with it?

Thank you very much.

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

consumerkey = OAuth::Consumer.new(
"TkALF8GIxPBfRq4rDcwJDSgqs",
"7lYLEBhysTFEqmJiWbadIv95DKinxe2vUzaOY4FeAwLIophDIc")
access
token = OAuth::Token.new(
"1665832231-2hCZcFT55aVdNzlOWcdD1CIsghhhiKoZIJu9E01",
"fLzMFmgZk51q84hYgLhRMACFKyUGfZ7MErXJh9jNwQekY")

baseurl = "https://api.twitter.com"
path = "/1.1/statuses/update.json"
address = URI("#{baseurl}#{path}")
request = Net::HTTP::Post.new address.requesturi
request.set
form_data(
"status" => "",
)

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

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

tweet = nil
if response.code == '200' then
tweet = JSON.parse(response.body)
puts "Successfully sent #{tweet["text"]}"
else
puts "Could not send the Tweet! " +
"Code:#{response.code} Body:#{response.body}"
end


0 votes

permalink

Hey there, just documenting I'm having the same issue. Double-checked and I pasted everything in correctly. I'm still assuming we're doing something wrong, just dont know what.

724 points
Picture
Submitted by
Guilherme Sa
almost 3 years ago

1 Comment

Picture Guilherme Sa almost 3 years ago

instead of double-clicking the text, then doing copy and paste, try manually selecting it then copying it from twitter's website. for some reason this solved it for me.


0 votes

permalink

For the final lesson, you need to go back to twitter and change your project's permission to 'read & right' for both your API keys & Access Tokens.

This will generate 4 new strings that will have to replace all your old strings stored in the consumerkey & accesstoken variables.

1648 points
558e38b9d3292f953800033c_234960162
Submitted by
tyler896
over 2 years ago