'Getting 401 Unauthorized error while requasting GitHub API

Good evening. My task is to create a website with react that will list through github users. I was following this instruction: https://docs.github.com/en/rest/users/users#get-a-user (for JS). My credentials:

async function searchUsers() {
  try {
    const octokit = new Octokit({
      auth: 'ghp_MY_PERSONAL_TOKEN',
      acceptstring: 'application/vnd.github.v3+json'
    })
    const response = await octokit.request(`GET /users/${name}`, {
      username: 'maria98kgm'
    });

    setUsers(response.data);
    setLoading(false);
    searchRepos();
    } 
    catch(e) {
      setLoading(false);
      setUsers('notFound');
      console.error('no such a user');
   }
}

Whenever i use it too long or push changes to github, it stops working and gives me 401 Unauthorized error: enter image description here

I cannot solve it for two days already, please help.



Solution 1:[1]

Explanation of the error.

The 404 status code means your authentication data are not the good ones.

How to solve it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 alexzerah