Home > database >  Issue of Authentication using GitHub API
Issue of Authentication using GitHub API

Time:12-05

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?

CodePudding user response:

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

CodePudding user response:

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.

  • Related