0

我尝试了以下代码来创建存储库。但它给出了 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"
}

4

2 回答 2

0

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

curl -i -H "Authorization: token 1f003ae8eab2feea72630d6f3150b921a522a868" -d '{ "name": "simp1", "private": true}' https://api.github.com/user/repos
于 2020-08-09T08:09:57.867 回答
0

您能否验证您的令牌已获得授权。请参阅授权 OAuth 应用程序

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

使用 JSON 格式化程序更新

"-d""{ \\
        \"name\": \"simp\", \\
        \"auto_init\": true, \\
        \"private\": true, \\
        \"gitignore_template\": \"nanoc\" \\
      }""\\
于 2020-08-09T06:00:02.553 回答