I am making a personal portfolio website. I want to get number of my public repositories. This is my first time using Github API which is why I tried many different things but none of them gives me perfect solution.
I have a file called access_token.txt. That stores my github access token. I can get it from .txt file and use it on my computer. It works correctly. But when it comes to commit my code in Github and try to show through Github pages, my Projects number is nothing
const username = "sevro49";
let token = "";
const filePath = '../../access_token.txt';
// Check if running on GitHub Pages or locally
if (window.location.hostname === "sevro49.github.io") {
// Running on GitHub Pages, use environment variable
token = process.env.ACCESS_TOKEN;
} else {
// Running locally, use fallback token value
const projectsElement = document.querySelector(".profile div:nth-child(2) .number-of-repos");
fetch(filePath)
.then(response => response.text())
.then(data => {
token = data;
fetch(`https://api.github.com/users/${username}`, {
headers: {
Authorization: `Bearer ${token}`,
},
})
.then(response => response.json())
.then(data => {
const numRepos = data.public_repos;
// Display the number of repositories on your website
projectsElement.innerText = `${numRepos}`;
})
.catch(error => console.error(error));
})
.catch(error => {
console.error('File could not be read', error);
});
}
I am adding my portfolio’s link if you want to check out the error: https://sevro49.github.io/My-Portfolio/public/index.html
2
Answers
If you want to show public repositories number you don’t any authentication, check this snippet
As @atrichkov mentioned, To get basic information about GitHub users (public repositories, date created, profile picture, etc..) you don’t need requests that includes token and authentication.
Here is an example of how to use GitHub API: