首页 > 解决方案 > 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 支持

标签: reactjsgithubreact-reduxaxiosgithub-api

解决方案


推荐阅读