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

OAuth2 Forum

View Course » View Exercise

1236 points
52ad96fe7c82ca447b008551_67114341
Submitted by
Laurence Marshall
almost 5 years ago

Exercise 5 - Creating Tokens For An Application

Hi there,

I seem to be a bit stuck on exercise 5, my current code is:

require 'httparty'
require 'json'

class GitHub
  API_CLIENT_ID     = '34a57b3c1f20e85a56ab'
  API_CLIENT_SECRET = '89060fce512d1042d168c3e71504ae35208cef1b'

  include HTTParty
  basic_auth "api-padawan", "GitHubPassw0rd"

  def create_token
    endpoint = "https://api.github.com/authorizations"
    postbody = {
        :clientid => API_CLIENT_ID,
        :clientsecret => API_CLIENT_SECRET
        }
    self.class.post endpoint, :body => post_body.to_json
  end
end

client = GitHub.new
response = client.create_token

puts response.parsed_response

Please could someone explain what I have done wrong?

Thanks.


7 votes

permalink

I've got it!

require 'httparty'
require 'json'

class GitHub
  API_CLIENT_ID     = '34a57b3c1f20e85a56ab'
  API_CLIENT_SECRET = '89060fce512d1042d168c3e71504ae35208cef1b'

  include HTTParty
  basic_auth "api-padawan", "GitHubPassw0rd"

  def create_token
    endpoint = "https://api.github.com/authorizations"
    post_body = {:client_id => API_CLIENT_ID, :client_secret => API_CLIENT_SECRET};
    self.class.post endpoint, :body => post_body.to_json
  end
end

client = GitHub.new
response = client.create_token

puts response.parsed_response

The code had to go along one line...
Plus, I added some underscores.
Hope this helps anyone else that gets stuck. :-)

1236 points
52ad96fe7c82ca447b008551_67114341
Submitted by
Laurence Marshall
almost 5 years ago

4 Comments

3790335aba1e1e2d72afb2a043f6e8ef?s=140&d=retro zechino over 4 years ago

How did you get around this error?

(ruby):4: warning: already initialized constant APICLIENTID
(ruby):5: warning: already initialized constant APICLIENTSECRET

52ad96fe7c82ca447b008551_67114341 Laurence Marshall over 4 years ago

Have you tried using a different browser?

Picture Stephen Aghaulor about 3 years ago

Oddly, You apparently can't use Ruby 1.9 hash syntax.

522475a480ff3350130018ed_410144016 postmanpat7 over 2 years ago

{complete}


0 votes

permalink

I don't think you've done wrong much, but the exercise doesn't give credit where it's due. :)

2108 points
52642d9437ea79ab654d4066e7ac1ed9?s=140&d=retro
Submitted by
modecode
almost 5 years ago

5 Comments

52ad96fe7c82ca447b008551_67114341 Laurence Marshall almost 5 years ago

I've got it! - Just added the code as an answer.

52642d9437ea79ab654d4066e7ac1ed9?s=140&d=retro modecode almost 5 years ago

Thanks for your help!

52ad96fe7c82ca447b008551_67114341 Laurence Marshall almost 5 years ago

No worries :-)

50cbeeb208952e54ac00308b_597869595 Skynet over 4 years ago

thanks,i was pulling my hair out. It was my missing underscores :(

51f78d179c4e9d6d7c0025ee_985617656 A yee sha sha over 4 years ago

XD


0 votes

permalink

your original code was missing underscores in the terms "clientid" and "clientsecret"

I think that's all that was causing the problem. Cheers.

682 points
Ce9e4805b5b114f82fb25ad52f852b5c?s=140&d=retro
Submitted by
mike
almost 5 years ago


0 votes

permalink

The correct answer is

require 'httparty'
require 'json'

class GitHub
APICLIENTID = '34a57b3c1f20e85a56ab'
APICLIENTSECRET = '89060fce512d1042d168c3e71504ae35208cef1b'

include HTTParty.get
basic_auth "api-padawan", "GitHubPassw0rd"

def createtoken
endpoint = "https://api.github.com/authorizations"
post
body = {:clientid => APICLIENTID, :clientsecret => APICLIENTSECRET};
self.class.post endpoint, :body => postbody.tojson
end
end

client = GitHub.new
response = client.create_token

puts response.parsed_response

This is the only way it will work. The directions and the api for the exercise are broken.

2197 points
53c963d9282ae3e81b00051f_841453022
Submitted by
Michael Rizzo
over 3 years ago