简体   繁体   English

重新申请 GitHub API 时出现 401 Unauthorized 错误

[英]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.我的任务是创建一个通过 github 用户列出的带有 react 的网站。 I was following this instruction: https://docs.github.com/en/rest/users/users#get-a-user (for JS).我正在遵循以下说明: https ://docs.github.com/en/rest/users/users#get-a-user(对于 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:每当我使用它太久或将更改推送到 github 时,它就会停止工作并给我401 Unauthorized错误: 在此处输入图像描述

I cannot solve it for two days already, please help.我已经两天没解决了,请帮忙。

Explanation of the error.错误的解释。

The 404 status code means your authentication data are not the good ones. 404 状态码意味着您的身份验证数据不是好的。

How to solve it.如何解决它。

You don't need any authentication for that endpoint.您不需要对该端点进行任何身份验证。 Here's a working example:这是一个工作示例:

 body { font-family: sans-serif; } button, input[type="text"] { font-size: 1rem; padding: 0.2rem; } pre { font-family: monospace; font-size: 1rem; } .vertical { display: flex; flex-direction: column; align-items: flex-start; gap: 0.5rem; }
 <!-- Babel seemed to have trouble with this, so I'm putting it on window --> <script type="module"> import {Octokit} from 'https://cdn.skypack.dev/octokit@1.7.1'; window.Octokit = Octokit; </script> <div id="root"></div><script src="https://unpkg.com/react@18.1.0/umd/react.development.js"></script><script src="https://unpkg.com/react-dom@18.1.0/umd/react-dom.development.js"></script><script src="https://unpkg.com/@babel/standalone@7.18.1/babel.min.js"></script> <script type="text/babel" data-type="module" data-presets="env,react"> // import * as ReactDOM from 'react-dom/client'; // import {useState} from 'react'; // import {Octokit} from 'octokit'; // This Stack Overflow snippet demo uses UMD modules instead of the commented import statments above const {useState} = React; const octokit = new Octokit(); function App () { const [user, setUser] = useState('maria98kgm'); const [userData, setUserData] = useState(undefined); const [error, setError] = useState(undefined); const updateUserData = async () => { try { const username = user.trim(); const response = await octokit.request(`GET /users/${username}`); setUserData(response.data); setError(undefined); } catch (ex) { if (ex instanceof Error) setError(ex); else console.error(ex); } }; const displayString = error ? error.toString() : userData ? JSON.stringify(userData, null, 2) : 'No data yet'; return ( <div> <div className="vertical"> <input type="text" onChange={ev => setUser(ev.target.value)} value={user} /> <button onClick={updateUserData}>Update user data</button> </div> <pre><code>{displayString}</code></pre> </div> ); } const reactRoot = ReactDOM.createRoot(document.getElementById('root')); reactRoot.render(<App />); </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM