0
votes

I'm building a simple application on react using axios and the GitHub API, but i'm having problems with the API authentication.

I've manage to get some requests, but I need to authenticate my access since I have a limited number of requests avaliable: "API rate limit exceeded for .... (But here's the good news: Authenticated requests get a higher rate limit.)"

I've generated a token for a GitHub account but I don't know how to use it. Here is what I tried:

    const fetchUsers = () => {
        axios.get('https://api.github.com/search/users?q=' + props.user, {
            'auth': {
              'username': my_user,
              'token': my_token,
            }
        })
        .then(response => {
            const users = response.data.items;
            const updatedUsers = users.map(users => {
                return {
                    ...users,
                }
            })
            setUsersLoaded(true)
            setUsers(updatedUsers)
        })
    }

Can I validate it with axios or do I have to work with a different approach?

1

1 Answers

2
votes

According to the documentation you have to add an authorization header.

axios.get('https://api.github.com/search/users?q=' + props.user, {
  'headers': {
    'Authorization': `token ${my_token}` 
  }
})

Otherwise github themselves actually offer a library called octokit which you can use instead of axios.