Using `curl` to create a repo on GitHub.com with t

2019-02-20 18:40发布

问题:

I'm having problems using curl to create a repo on my GitHub.com account (no ssh).

I have created a basic static local site using Jekyll which I want to push to the GitHub.com repo (I want to host the site on <username>.github.io and according to the instructions on GitHub pages the repo must have the same name as the folder containing the local site.

I have two-factor authentication enabled for the GitHub login, so following this post I am passing an OTP/token (generated on Applications settings in the request header using the option -H "X-GitHub-OTP":<token>. I can log in (this post previously raised an issue about the Github login not working, which was due to not closing the string "X-GitHub-OTP" properly), but now the login works but I now receive a response with a 400 code. Here is the curl command I am used:

$username="<username>"
$reponame="$username.github.io"
$token="<token>"
$apidom="https://api.github.com/user/repos/"
$curl -u $username -H '{"X-GitHub-OTPOTP":$token}' -d '{"name":$reponame,"description":$repodesc}' $apidom

and the response message:

<p><strong>We didn't receive a proper request from your browser.</strong></p>
<p>Sorry about that. Please try refreshing and contact us if the problem persists.</p>
<div id="suggestions">
<a href="https://github.com/contact">Contact Support</a> &mdash;
<a href="https://status.github.com">GitHub Status</a> &mdash;
<a href="https://twitter.com/githubstatus">@githubstatus</a>
</div>

回答1:

Since you have two-factor authentication enabled, you will need to send a special HTTP header:

In addition to the Basic Authentication credentials, you must send the user’s authentication code (i.e., one-time password) in the X-GitHub-OTP header. Because these authentication codes expire quickly, we recommend using the Authorizations API to create an access token and using that token to authenticate via OAuth for most API access.

Alternately, you can create access tokens from the Personal Access Token section of your application settings page.