通过REST为Github项目创建里程碑

时间:2019-10-24 20:31:22

标签: rest github github-api

我想使用Github的REST API来管理某些存储库的里程碑和标签,但是我一直无法通过REST创建新的里程碑或标签。我想念什么?

我为我的帐户启用了2FA,并且还创建了一个个人访问令牌,如Other Authentication Methods

中所述

例如,我尝试使用此cURL命令创建一个新的里程碑:

curl -X POST -v \
  -H "Authorization: token MyPersonalAccessToken" \
  -H "Content-Type: application/json" \
  -H "Accept: application/vnd.github.v3+json" \
  -d '{"title": "1", "state": "open", "description": "Milestone", "due_on": "2025-10-09T23:39:01Z"}' \
  https://api.github.com/repos/obfischer/boxsack/milestones

我尝试了其他各种变体,但似乎都错了。至少我总是只得到一个404 Not Found。根据Github文档,这也可能意味着我的身份验证失败,因为Github返回了404 Not Found响应代码来保护用户数据,而不是返回401 Unauthorized

但是我非常确定通过“人员访问令牌”进行的身份验证可以正常工作,因为我可以在响应中看到用户特定的速率限制标头:

X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 4999

有人知道为什么我无法通过REST创建里程碑吗?

1 个答案:

答案 0 :(得分:1)

也许您的个人访问令牌没有repo scope。我刚刚测试过使用不带该范围的令牌发送请求,并且它还会返回404:

< HTTP/1.1 404 Not Found
< Date: Sat, 26 Oct 2019 11:07:31 GMT
< Content-Type: application/json; charset=utf-8
< Content-Length: 125
< Server: GitHub.com
< Status: 404 Not Found
< X-RateLimit-Limit: 5000
< X-RateLimit-Remaining: 4989
< X-RateLimit-Reset: 1572091544
< X-OAuth-Scopes: [Here there should be repo] <================
< X-Accepted-OAuth-Scopes: 
< 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
< 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'
< X-GitHub-Request-Id: 87B2:23EED:36F2B7F:42647AE:5DB428F3
< 
{
  "message": "Not Found",
  "documentation_url": "https://developer.github.com/v3/issues/milestones/#create-a-milestone"
}