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

OAuth2 Forum

View Course » View Exercise

1146 points
746397352dc2cebf3ae75b97b2ee5ecd?s=140&d=retro
Submitted by
bguiz
about 4 years ago

Github API has changes - now requires new header

Instruction say to simply run this:

require 'httparty'

token = "e6a75d6b6d7b0fdd0eee93fd3c141d808852a0e9"

user = HTTParty.get(
            "https://api.github.com/user", 
            :headers => {
                "Authorization" => "token #{token}"
                })

puts "Hi, my username is #{user['login']}"

However it will not work, because:

{"message":"Missing or invalid User Agent string. See http://developer.github.com/v3/#user-agent-required"}

GitHub's API now requires a user agent string to be specified in the request header. So change this to:

require 'httparty'

token = "e6a75d6b6d7b0fdd0eee93fd3c141d808852a0e9"

user = HTTParty.get(
            "https://api.github.com/user", 
            :headers => {
                "Authorization" => "token #{token}",
                "User-Agent" => "codecademy"
                })

puts "Hi, my username is #{user}"

... and then it will work


7 votes

permalink

I cannot get this to work.

If I use the code that comes with the lesson, the last line being

puts "Hi, my username is #{user["login"]}"

The result display just shows:

Hi, my username is
nil

And Codecademy shows the error:

Oops, try again. It looks like your code didn't print the correct username! Click the red Reset button, then click run.

But if I change that last line to be (as shown in the first post of this thread):

puts "Hi, my username is #{user}"

I get:

Hi, my username is {"message"=>"Bad credentials", "documentation_url"=>"http://developer.github.com/v3"}

How can I fix the bad credentials?

660 points
87f291972af964b2c1adb34e03481251?s=140&d=retro
Submitted by
alondono
over 3 years ago

6 Comments

54dad94d9113cb116c000ddd_987464548 Emily Martinez over 3 years ago

I'm having this problem too.

53056feb548c358e1d001c9e_887221709 Carlisia Campos over 3 years ago

Same here

Picture Sean Maloney over 3 years ago

As am I

Ee86c56e97957b919afb55846562a72a?s=140&d=retro brodeezy5 over 3 years ago

Same here....

Picture Teddy Ditchek over 3 years ago

same here...

2cda9ab5a02842e6ef7ad8974759e388?s=140&d=retro Broune over 3 years ago

Same here..


4 votes

permalink

I changed the last line to:
puts "Hi, my username is #{user[login]}"
I still get error in console, but it passes the lesson.

1058 points
56b7ca851301e27e09000392_344526546
Submitted by
M
over 3 years ago


3 votes

permalink

THIS WORKED FOR ME:
require 'HTTParty'

token = "e6a75d6b6d7b0fdd0eee93fd3c141d808852a0e9"

user = HTTParty.get "https://api.github.com/user",
:headers => {
"Authorization" => "token #{token}",
"User-Agent" => "codecademy"

}

puts "Hi, my username is #{user["login"]}"

1333 points
Ae5b33614a9da769bd5a3d5341308bc1?s=140&d=retro
Submitted by
PT83333
about 3 years ago

3 Comments

9eac8ff2f574a2601d1ca099c289fb5c?s=140&d=retro thelogicofpi almost 3 years ago

errors are good, right? passed with weird errors

516b4ec67af648e62f765b2224f80181?s=140&d=retro Austin over 2 years ago

Using this it passes with cannot load such file -- HTTParty and when I lowercase it I get errors.

Picture Jesus Gonzalez over 2 years ago

this work for pass but it give me a error. so i did work any before at least now i finish.


2 votes

permalink

This still doesn't work for me.

2931 points
5923b368854be9060200039d_763435116
Submitted by
Michael
over 3 years ago


2 votes

permalink

This course is a non-starter! Codecademy really should move it to the 'dead course' file if it can't maintain its currency!

3606 points
F8605524ef00afa5612d8fe5fdc1c0c2?s=140&d=retro
Submitted by
tele741
over 3 years ago


2 votes

permalink

You can switch the #{user["login"]} out with api-padawan. Which I believe is the username the lesson is looking for in order to pass.

Still, as of 4/17/14 the OAuth token provided in the lesson (e6a75d6b6d7b0fdd0eee93fd3c141d808852a0e9) is sending back a 401 response with the "Bad credentials" error.

If you want to see the api working, you could always generate your own Github api token and make a new token variable, pass that, and puts out a string of whatever you want from the response.

1643 points
5806788ee92d41b124000378_979316517
Submitted by
Tim
over 3 years ago


1 vote

permalink

Wow, thanks a lot!

1746 points
Daa8867ef849b3c26fb8773a4bcd1775?s=140&d=retro
Submitted by
Finn
about 4 years ago

1 Comment

Picture Nate Gay about 4 years ago

Yes, thank you!