2

I am trying to authenticate a user by GitHub API. I've tried on two methods, Authentication via username and password and via personal access token(PAT) like this;

curl -u devmania1223 https://api.github.com/user   /*via username and password*/

curl -u ghp_I60uniHdf6UKDCkyde1InP7kwRwsw2fD0wx https://api.github.com/user   /*via personal access token*/

Inputted username and password, PAT correctly, but the response is not right.

{
  "message": "Requires authentication",
  "documentation_url": "https://docs.github.com/rest/reference/users#get-the-authenticated-user"
}

So, What's wrong with curl command?

2 Answers 2

1

Try and use the token as password:

curl -i -u your_username:$token https://api.github.com/users/octocat

Don't forget the other option: gh (the GitHub cli/cli), using gh auth login

# authenticate against github.com by reading the token from a file
$ gh auth login --with-token < mytoken.txt
Sign up to request clarification or add additional context in comments.

Comments

1

Let's say that your personal access token is 55a6f290558d11ecbeaf787b8ab956b4. Now, make a request using the GitHub API, put the token into the "Authorization" header:

/usr/bin/curl -H "Authorization: Bearer 55a6f290558d11ecbeaf787b8ab956b4" https://api.github.com/user

Why? Well, the type of token that GitHub gives to you is called "OAuth 2.0" token, and you just have to use it this way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.