【问题标题】:Using `curl` to create a repo on GitHub.com with two-factor authentication使用“curl”在 GitHub.com 上创建具有双重身份验证的 repo
【发布时间】:2015-03-01 02:21:40
【问题描述】:

我在使用 curl 在我的 GitHub.com 帐户(无 ssh)上创建存储库时遇到问题。

我使用 Jekyll 创建了一个基本的静态本地站点,我想将其推送到 GitHub.com 存储库(我想在 <username>.github.io 上托管该站点,并且根据 GitHub 页面上的说明,存储库必须具有与包含本地站点的文件夹同名。

我为 GitHub 登录启用了双重身份验证,因此在 this post 之后,我传递了一个 OTP/令牌(使用选项 -H "X-GitHub-OTP":<token> 在请求标头中的 Applications settings 上生成。我可以登录(这个之前的帖子提出了一个关于 Github 登录不起作用的问题,这是由于没有正确关闭字符串 "X-GitHub-OTP"),但现在登录有效,但我现在收到带有 400 代码的响应。这是我的 curl 命令我用过:

$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

以及响应消息:

<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>

【问题讨论】:

    标签: git http curl github


    【解决方案1】:

    由于您启用了双重身份验证,因此您需要send a special HTTP header:

    除了基本身份验证凭据之外,您还必须在 X-GitHub-OTP 标头中发送用户的身份验证代码(即一次性密码)。由于这些身份验证代码很快过期,我们建议使用授权 API 到 create an access token 并使用该令牌到 authenticate via OAuth 进行大多数 API 访问。

    或者,您可以从application settings page 的个人访问令牌部分创建访问令牌。

    【讨论】:

    • 谢谢,我使用设置创建了个人令牌,但它仍然无法正常工作,使用相同的 curl 命令格式(使用正确的密码和令牌)我现在收到此消息:{ "message": "Must specify two-factor authentication OTP code.", "documentation_url": "https://developer.github.com/v3/auth#working-with-two-factor-authentication" }
    • @ramius,该错误消息中的 URL 与我在答案中链接到的 URL 相同。您能否编辑您的问题以明确说明您为生成此消息所做的工作?
    • 嗨,当然,没关系。 This question 似乎相同。我稍后再看看。
    • 谢谢,但我在curl 语句的 HTTP 响应中遇到了另一个问题,如我更新的问题中所述。任何帮助将不胜感激。
    • @ramius,我刚看到这条评论。但你已经接受了答案……现在一切正常吗?
    【解决方案2】:

    双重身份验证

    curl -u "YOUR_USERNAME_HERE" \
        -H "X-Github-OTP:YOUR_OTP_CODE_HERE" \
        https://api.github.com/user/repos
        -d '{"name": "test"}'
    

    普通认证

    curl -u "YOUR_USERNAME_HERE" \
        -H https://api.github.com/user/repos \
        -d '{"name":"Your repo name here"}'
    

    【讨论】:

    • 欢迎来到 SO。以后请format你的答案。
    猜你喜欢
    • 2016-11-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2011-07-06
    • 1970-01-01
    • 2019-03-23
    • 2021-03-31
    • 1970-01-01
    相关资源
    最近更新 更多