0

GitHub seems to have made updates since end of 2021.

https://developer.github.com/changes/2020-02-10-deprecating-auth-through-query-param/

I have followed numerous resources where the below code increases the amount of requests one can do per hour. Now the below request does not work. Instead the documentation says to use CURL, so for instance the below works in a terminal:

curl -u client_id:secret_key https://api.github.com/users/<username>

I want to do this in JavaScript, I am playing around with a GitHub user finder app in JavaScript. Can someone please show me how I can get this to actually work. The code I am using is below.

TL:DR: I can access the github API using the code below and receive a JSON object to display, but it's limited to 60 requests per hour. GitHub documentation says that since end of 2021 query parameters are not allowed anymore so I'm lost now. How can I do this in JavaScript now?

const client_id = "df2429c311a306c35233";
const client_secret = "5c23233326680aa21629451a6401d36ec";

const fetchUsers = async (user) => {
    const api_call = await fetch(`https://api.github.com/users/${user}?client_id=df5429c311a306c356f4&
    client_secret=${client_secret}`);

    const data = await api_call.json();
    return {data};

};

EDIT/UPDATE:

const inputValue = document.querySelector("#search");
const searchButton = document.querySelector(".searchButton");
const nameContainer = document.querySelector(".main__profile-name");
const unContainer = document.querySelector(".main__profile-username");
const reposContainer = document.querySelector(".main__profile-repos");
const urlContainer = document.querySelector(".main__profile-url");

const client_id = "<user_id>";
const client_secret = "<client_secret>";

const headers = {
    'Authorization': 'Basic ' + (new Buffer(client_id + ':' + client_secret).toString('base64'))
}

const fetchUsers = async (user) => {
    const api_call = await fetch(`https://api.github.com/users/${user}`, {
        method: 'GET',
        headers: headers
    });

    const data = await api_call.json();
    return {data};

};

const showData = () => {
    fetchUsers(inputValue.value).then((res) => {
        console.log(res);

        nameContainer.innerHTML = `Name: <span class="main__profile-value">${res.data.name}</span>`

        unContainer.innerHTML = `Username: <span class="main__profile-value">${res.data.login}</span>`

        reposContainer.innerHTML = `Repos: <span class="main__profile-value">${res.data.public_repos}</span>`

        urlContainer.innerHTML = `Url: <span class="main__profile-value">${res.data.url}</span>`

    })
};

searchButton.addEventListener("click", () => {
    showData();
})
2
  • 2
    And now you need to rotate your GitHub credentials, because you've shared them publicly. Commented Apr 26, 2022 at 10:21
  • Yeah I noticed, major goof. I was about to change them but missed that. I've scrambled the numbers in the id and secret there now though. Commented Apr 26, 2022 at 10:23

1 Answer 1

4

Those behave as username and password of the basic authentication type. Hence your Api request should have the following header.

const headers = {
      'Authorization': 'Basic ' + btoa(CLIENT_ID + ':' + CLIENT_SECRET)
    }

Please note that btoa function is being used because browsers don't have a native support of Buffer. If btoa throws error then try with window.btoa and use it like

const response = await fetch(url, {method:'GET',
    headers: headers,
   })
Sign up to request clarification or add additional context in comments.

6 Comments

Right, I've done that now, I'm not getting any response back now from the API. Something is broken, can you please check the code in the update above. I get Uncaught ReferenceError: Buffer is not defined
can you please mention the env you are using to execute this. Is it on a node js env ? Also please try consoling the values on each step and share the error message if any
It's not nodejs env, it's just a vanilla JavaScript app. The error message I get is this: Uncaught ReferenceError: Buffer is not defined at app.js:12:38 , I have pasted the full code now in the update, please check
Gotcha! Browsers don't have a native support of Buffer. Lemme look through some other options to achieve this according to your needs
Please check the updated answer and do let me know the result
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.