我正在尝试在我的 React 应用程序中实现 Github 0auth,我为此使用了 react-login-github 包和@octokit/rest
import LoginGithub from 'react-login-github'; import { Octokit } from '@octokit/rest';
这是我的登录按钮组件
<LoginGithub clientId={process.env.REACT_APP_GITHUB_CLIENT_ID}
onSuccess={onSuccess}
onFailure={onFailure}
redirectUri={`http://localhost:3000`}
className='group border-black border-2 p-3 rounded-full hover:bg-black hover:text-white active:bg-black active:text-white duration-300 hover:-translate-y-1 active:-translate-y-1 font-semibold '
scope='read:user%20user:email'
>
<i className="fa-brands fa-github fa-2xl pr-4"></i> Continue With Github
</LoginGithub>
这是我的 onSuccess 函数
const onSuccess = response => {
getAccessToken(response.code)
}
这是我的getAccessToken功能
const getAccessToken = async (code) => {
console.log(code);
try {
const response = await octokit.request('POST /login/oauth/access_token', {
headers: {
Accept: 'application/json'
},
data: {
client_id: process.env.REACT_APP_GITHUB_CLIENT_ID,
client_secret: process.env.REACT_APP_GITHUB_CLIENT_,
code: code
}
});
const accessToken = response.data.access_token;
console.log(accessToken);
setAccessToken(accessToken)
// Use the access token in your application
// ...
} catch (error) {
// Handle error
console.error('Error getting access token:', error);
}
};
我收到 HttpError
Error getting access token: HttpError: Not Found
我成功地从 github 获取了代码,但是 getAccessToken 似乎抛出了这个错误。
我尝试了多种不同的方法,但似乎没有任何效果。 我是 0auth 功能的新手。