reactjs - Github api 显示 401 错误未经授权的访问
问题描述
我是新来的反应和github。我正在关注一个教程,我们向 github api 发送请求并获取配置文件。你必须创建一个个人 github 令牌并使用 axios 来获取搜索配置文件。一天前它工作正常。但是现在它显示 401 未经授权的获取错误:
这是我正在使用的代码:
const github = axios.create({
baseURL: "https://api.github.com",
timeout: 1000,
headers: { Authorization: process.env.REACT_APP_GITHUB_TOKEN },
});
class App extends Component {
state = {
users: [],
loading: false,
alert: null,
};
static propTypes = {
searchUsers: PropTypes.func.isRequired,
};
searchUsers = async (text) => {
this.setState({ loading: true });
const res = await github.get(`/search/users?q=${text}`);
this.setState({ users: res.data.items, loading: false });
};
//Clear users from state
clearUsers = () => this.setState({ users: [], loading: false });
//Set Alert
setAlert = (msg, type) => {
this.setState({ alert: { msg, type } });
setTimeout(() => this.setState({ alert: null }), 5000);
};
render() {
const { users, loading } = this.state;
return (
<div className="App">
<Navbar />
<div className="conatiner">
<Alert alert={this.state.alert} />
<Search
searchUsers={this.searchUsers}
clearUsers={this.clearUsers}
showClear={users.length > 0 ? true : false}
setAlert={this.setAlert}
/>
<Users loading={loading} users={users} />
</div>
</div>
);
}
}
export default App;
我在 .env.local 中的 github 令牌如下:
REACT_APP_GITHUB_TOKEN='令牌'
请帮助我。我不明白它以前工作过,现在我不知道为什么 401 支持
解决方案
推荐阅读
- visual-studio-code - Is there a way to save all files, commit and upload in one command in Visual Studio Code
- google-compute-engine - GCE 实例停止工作 1 小时
- c++ - 'const' 关键字是如何工作的?
- java - Javax 持久性在生产级别将 IDENTITY 更改为 SEQUENCE
- python-2.x - 理解 Python 2.x 中的深拷贝和浅拷贝
- pip - Airflow 1.10 安装失败
- javascript - Javascript 不会影响 HTML 元素,除非它低于它
- java - 如何解决Android中左右导航视图应用崩溃的问题?
- java - 计算java字符串中的句子数
- java - 如何验证发送到受监控邮箱的电子邮件的来源和真实性