【问题标题】:Curl command for creating Github Repository is not working用于创建 Github 存储库的 Curl 命令不起作用
【发布时间】:2020-11-29 00:42:00
【问题描述】:

我尝试了以下代码来创建存储库。但它给出了 JSON 解析问题。这是什么原因造成的?我从 https://docs.github.com/en/rest/reference/repos 中获取了这段代码,并使用 requests.post 方法在 python 代码中进行了尝试。但这也给出了同样的错误Problem Parsing JSON。这是错误的请求还是 Github API 内部的解析问题。

curl -i -H "Authorization: token MYACCESSTOKENHERE" \
    -d '{ \
        "name": "simp", \
        "auto_init": true, \
        "private": true, \
        "gitignore_template": "nanoc" \
      }' \
    https://api.github.com/user/repos

输出:

HTTP/1.1 400 Bad Request
Date: Sun, 09 Aug 2020 05:14:23 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 159
Server: GitHub.com
Status: 400 Bad Request
X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 4956
X-RateLimit-Reset: 1596950918
X-OAuth-Scopes: admin:gpg_key, admin:org, admin:org_hook, admin:repo_hook, gist, notifications, read:packages, repo, user, workflow, write:discussion, write:packages
X-Accepted-OAuth-Scopes: public_repo, repo
X-GitHub-Media-Type: github.v3; format=json
Access-Control-Expose-Headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, Deprecation, Sunset
Access-Control-Allow-Origin: *
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
X-Frame-Options: deny
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
Content-Security-Policy: default-src 'none'
Vary: Accept-Encoding, Accept, X-Requested-With
X-GitHub-Request-Id: B649:25C9:CDEF3A:117E0EE:5F2F862F

{
  "message": "Problems parsing JSON",
  "documentation_url": "https://docs.github.com/rest/reference/repos#create-a-repository-for-the-authenticated-user"
}

【问题讨论】:

    标签: json github curl github-api github-api-v3


    【解决方案1】:

    能否请您验证您的令牌已获得授权。 请参考Authorizing OAuth Apps

    curl -H "Authorization: token OAUTH-TOKEN" <https://<your_repo_endpoint> -I
    

    使用 JSON 格式化程序更新

    "-d""{ \\
            \"name\": \"simp\", \\
            \"auto_init\": true, \\
            \"private\": true, \\
            \"gitignore_template\": \"nanoc\" \\
          }""\\
    

    【讨论】:

    • 我使用的令牌不是问题,否则会指出授权错误。但问题出在 JSON 的正文中。
    • 这没有提供问题的答案。一旦你有足够的reputation,你就可以comment on any post;相反,provide answers that don't require clarification from the asker。 - From Review
    • @RNanthak 尝试使用 JSON 解析器查看格式,并发现它如下所示 "-d""{ \\ \"name\": \"simp\", \\ \"auto_init \": true, \\ \"private\": true, \\ \"gitignore_template\": \"nanoc\" \\ }""\\
    • 谢谢@albertsebastian。我找到了。现在我使用curl -i -H "Authorization: token 1f003ae8eab2feea72630d6f3150b921a522a868" -d '{ "name": "simp1", "private": true}' https://api.github.com/user/repos。删除不必要的行后,它起作用了。
    • 嗨@RNanthak 很高兴收到您的回复,您已经找到了正确的格式。
    【解决方案2】:

    问题在于 JSON 格式。从 curl 命令中删除不需要的行和空格后,它起作用了。

    curl -i -H "Authorization: token 1f003ae8eab2feea72630d6f3150b921a522a868" -d '{ "name": "simp1", "private": true}' https://api.github.com/user/repos
    

    【讨论】:

      猜你喜欢
      • 2022-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-31
      • 2012-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多