Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I not authenticate with the GitHub REST API using Axios?

I'm sort of new to REST..

For full disclosure, I'm running this code inside of a Netlify Lambda function and testing via netlify-lambda.

My curl command works:

 curl -u "<username>:<password>" https://api.github.com/repos/<username>/<reponame>

But when I attempt a get request via axios I'm getting a 404 (which according to github docs implies an auth issue). This is what I'm doing (also doesn't work without the custom headers, I've just been trying random things).

axios({
    method: "get",
    url: `https://api.github.com/repos/${user}/<reponame>/`,
    headers: {
        Authorization: `Bearer ${githubToken}`,
        "Content-Type": "application/json"
    },
    auth: {
        username: user,
        password: pass
    }
    })
    .then(res => {
        callback(null, {
            statusCode: 200,
            body: JSON.stringify(res.data)
        });
    })
    .catch(err => {
        callback(err);
    });

One thing I noticed was that it seems axios was taking my username and password and prepending them to the url i.g. https://<username>:<password>@api.github.com/repos/<username>/<reponame>

Is this how auth should be sent over?

like image 452
Esten Avatar asked Apr 28 '19 22:04

Esten


2 Answers

If you already have a token you don’t need user/pass, just add the token to the header.

like image 137
Νικόλαος Μανωλακος Avatar answered Oct 23 '22 12:10

Νικόλαος Μανωλακος


I shouldn't have had a trailing forward slash at the end of my URL.

like image 4
Esten Avatar answered Oct 23 '22 13:10

Esten