1

When I create a new Github repository by using the curl command as shown below, it always asks me to enter the password.

curl -u "$(git config user.name)" https://api.github.com/user/repos -d "{\"name\":\"$1\"}" | grep -q "Bad credentials"

Here is my question. How to use SSH to login Github and create a new repository by using the ssh keychain in the command line?

  • See https://developer.github.com/v3/#authentication. – ElpieKay Aug 15 '20 at 08:03
  • @ElpieKay Thanks, I read the page and changed the code as `curl -u "Authorization: $(cat ~/.ssh/id_rsa.pub)" https://api.github.com/user/repos -d {\"name\":\"test\"}`, but it returned the massage as: `{ "message": "Bad credentials", "documentation_url": "https://docs.github.com/rest" }` What did I miss? – Teenage_Programmer Aug 15 '20 at 08:22
  • You are calling a github api. It's an HTTP request. It needs a token or a password instead of the public key. – ElpieKay Aug 15 '20 at 08:24
  • To generate a token, see https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token. – ElpieKay Aug 15 '20 at 08:31
  • @ElpieKay Thank you for your patience. I have generated the personal access tokens and still got the `"message": "Bad credentials"`. So sad. – Teenage_Programmer Aug 15 '20 at 08:42
  • If you provide a token, `-u "$(git config user.name)"` is not needed. – ElpieKay Aug 15 '20 at 08:43
  • @ElpieKay Thank you. Yes, I did erased the `-u "$(git config user.name)"`, and the command become `curl "Authorization: token MY_TOKEN_NUMBERS" https://api.github.com/user/repos/ -d {\"name\":\"test\"}`. However, the message is shown as `curl: (3) URL using bad/illegal format or missing URL` – Teenage_Programmer Aug 15 '20 at 08:52
  • `curl -H "Authorization: token MY_TOKEN_NUMBERS" ...`. You missed `-H` or `--header`. – ElpieKay Aug 15 '20 at 08:53
  • @ElpieKay I also run the `curl -H "Authorization: token MY_TOKEN_NUMBERS" ...`. The `"message": "Not Found"` shows up. – Teenage_Programmer Aug 15 '20 at 08:55
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/219858/discussion-between-elpiekay-and-teenage-programmer). – ElpieKay Aug 15 '20 at 08:57
  • @ElpieKay Thank you so much. I knew where I did wrong. After the `repos`, `/` should not exist. Therefore, the command should be `curl -H "Authorization: token MY_TOKEN_NUMBERS" https://api.github.com/user/repos -d {\"name\":\"test\"} ` – Teenage_Programmer Aug 15 '20 at 09:00

1 Answers1

3

Keep in mind the user.name/user.email config settings have nothing to do with:

  • authentication (they are used for commit authorship only)
  • your GitHub account (you can set your user.name to be the same as your GitHub account name, but user.name can be anything you want, really)

Bu using a curl -H "Authorization: token MY_TOKEN_NUMBERS" as recommended by ElpieKay, you bypass completely the need to provide a "user name".
The PAT (Personal Access Token) is enough for GitHub to authenticate you.

Anything with 'ssh' in it (like ~/.ssh/id_rsa.pub) would apply only for SSH URL (git@github.com:<me>/<myRepo>) and has nothing to do with HTTPS-based curl commands.
SSH would not apply to api.github.com calls anyway, as I mentioned in 2013.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250