Create Github repository using command line + API

1

Please provide working code that creates a Github repository using the API.

I have tried different versions of the following:

curl 'https://api.github.com/users/repos?client_id= myusername&client_secret=abcdefghijklmnopqrstuvwxyz1234567890abcd'
curl -u 'myusername' https://api.github.com/users/repos -d '{"name":"my-new-repo"}'

But I always get the following error:

"{
  \"message\": \"Bad credentials\",
  \"documentation_url\": \"https://developer.github.com/v3\"
}"

Mowzer

Posted 2016-04-20T19:38:55.170

Reputation: 1 109

Answers

2

I see a few problems in the curl commands you've posted:

  • You're using the path /users/repos. That will return the information of the user repos, instead of modifying a user's repositories. You need to use /user/repos (delete the s from users).
  • The field client_secret isn't for your username, but for the client ID you received when you registered your application in GitHub's API.
  • Your code field is missing too, if you're using the Web Application Flow.

You can find more details on GitHub's API OAuth section.

However, if you're just looking for a ready-to-work version, this one worked for me:

curl -H "Authorization: token YOUR_TOKEN" --data '{"name":"YOUR_REPO_NAME"}' https://api.github.com/user/repos

Although I'm pretty sure there are other simpler and cleaner ways to do this.

Hewbot

Posted 2016-04-20T19:38:55.170

Reputation: 1 604

More details on getting the token: http://superuser.com/a/1069606/471181

– Mowzer – 2016-04-25T16:27:38.703

4

One clarification on @Hewbot's answer.

One can use personal access tokens instead of OAuth.

The YOUR_TOKEN value in the "Authorization: token YOUR_TOKEN" section of the curl command can be obtained as follows.

  1. Upper right corner, click User
  2. Settings
  3. Personal Access Tokens
  4. For scopes, select Repos.
  5. Create New Token.

For more details, click here and here.

Mowzer

Posted 2016-04-20T19:38:55.170

Reputation: 1 109

3

This might help someone out.

If you use a restful client with a GUI, this is basically the configuration that you need to get your repo created:

enter image description here

Make sure you add your token after the word token.

benscabbia

Posted 2016-04-20T19:38:55.170

Reputation: 370