【问题标题】:Cannot get 'download_count', or any assets, from assets list in github API for a release on a public repo无法从 github API 中的资产列表中获取“download_count”或任何资产以在公共仓库上发布
【发布时间】:2020-12-05 00:14:19
【问题描述】:

我想计算公共存储库上发布的下载量。 https://docs.github.com/en/free-pro-team@latest/rest/reference/repos#list-release-assets 的文档 表示下面的资产请求应该有一个变量"download_count:"

这是我在一个新创建的测试组织中获得的小型公共测试存储库,其中有 2 个已知的 v1.0 版本下载:

curl https://api.github.com/repos/test10010000/countMyReleases/releases/tags/v1.0

回复是:

  "url": "https://api.github.com/repos/test10010000/countMyReleases/releases/34859773",
  "assets_url": "https://api.github.com/repos/test10010000/countMyReleases/releases/34859773/assets",
  "upload_url": "https://uploads.github.com/repos/test10010000/countMyReleases/releases/34859773/assets{?name,label}",
  "html_url": "https://github.com/test10010000/countMyReleases/releases/tag/v1.0",
  "id": 34859773,
  "author": {
    "login": "breckbaldwin",
    "id": 6377185,
    "node_id": "MDQ6VXNlcjYzNzcxODU=",
    "avatar_url": "https://avatars2.githubusercontent.com/u/6377185?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/breckbaldwin",
    "html_url": "https://github.com/breckbaldwin",
    "followers_url": "https://api.github.com/users/breckbaldwin/followers",
    "following_url": "https://api.github.com/users/breckbaldwin/following{/other_user}",
    "gists_url": "https://api.github.com/users/breckbaldwin/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/breckbaldwin/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/breckbaldwin/subscriptions",
    "organizations_url": "https://api.github.com/users/breckbaldwin/orgs",
    "repos_url": "https://api.github.com/users/breckbaldwin/repos",
    "events_url": "https://api.github.com/users/breckbaldwin/events{/privacy}",
    "received_events_url": "https://api.github.com/users/breckbaldwin/received_events",
    "type": "User",
    "site_admin": false
  },
  "node_id": "MDc6UmVsZWFzZTM0ODU5Nzcz",
  "tag_name": "v1.0",
  "target_commitish": "main",
  "name": "Test Count Downloads",
  "draft": false,
  "prerelease": false,
  "created_at": "2020-12-04T23:28:39Z",
  "published_at": "2020-12-04T23:29:18Z",
  "assets": [

  ],
  "tarball_url": "https://api.github.com/repos/test10010000/countMyReleases/tarball/v1.0",
  "zipball_url": "https://api.github.com/repos/test10010000/countMyReleases/zipball/v1.0",
  "body": ""
}

所以我得到了发布的数据,但请注意资产列表是空的。我按照文档中的说明运行资产的显式请求(上面的返回数据中也提供了这个 url):

curl https://api.github.com/repos/test10010000/countMyReleases/releases/34859773/assets

这会返回一个空列表:

[

]

添加我的授权令牌并使用 -i 查看返回标头,如下所示:

curl -i -u breckbaldwin:$github_token https://api.github.com/repos/test10010000/countMyReleases/releases/34859773/assets

我明白了:

HTTP/1.1 200 OK
Server: GitHub.com
Date: Fri, 04 Dec 2020 23:44:15 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 5
Status: 200 OK
Cache-Control: private, max-age=60, s-maxage=60
Vary: Accept, Authorization, Cookie, X-GitHub-OTP
ETag: "7ac3b1740ea68a78ed14bc8d160dae63ad1710fffb20a10910ae02fff4661e1d"
X-OAuth-Scopes: admin:enterprise, admin:gpg_key, admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete:packages, delete_repo, gist, notifications, repo, user, workflow, write:discussion, write:packages
X-Accepted-OAuth-Scopes: repo
X-GitHub-Media-Type: github.v3; format=json
X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 4990
X-RateLimit-Reset: 1607125659
X-RateLimit-Used: 10
Access-Control-Expose-Headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, 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: AE6A:5AF5:108649D:1BE42F1:5FCAC9CF

[

]

仍然没有"download_count:"。我期待一个看起来像这样的资产列表(来自不同仓库的文档):

[
  {
    "url": "https://api.github.com/repos/octocat/Hello-World/releases/assets/1",
    "browser_download_url": "https://github.com/octocat/Hello-World/releases/download/v1.0.0/example.zip",
    "id": 1,
    "node_id": "MDEyOlJlbGVhc2VBc3NldDE=",
    "name": "example.zip",
    "label": "short description",
    "state": "uploaded",
    "content_type": "application/zip",
    "size": 1024,
    "download_count": 42,
    "created_at": "2013-02-27T19:35:32Z",
    "updated_at": "2013-02-27T19:35:32Z",
    "uploader": {
      "login": "octocat",
      "id": 1,
      "node_id": "MDQ6VXNlcjE=",
      "avatar_url": "https://github.com/images/error/octocat_happy.gif",
      "gravatar_id": "",
      "url": "https://api.github.com/users/octocat",
      "html_url": "https://github.com/octocat",
      "followers_url": "https://api.github.com/users/octocat/followers",
      "following_url": "https://api.github.com/users/octocat/following{/other_user}",
      "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
      "organizations_url": "https://api.github.com/users/octocat/orgs",
      "repos_url": "https://api.github.com/users/octocat/repos",
      "events_url": "https://api.github.com/users/octocat/events{/privacy}",
      "received_events_url": "https://api.github.com/users/octocat/received_events",
      "type": "User",
      "site_admin": false
    }
  }
]

注意"download_count": 42,,这是我要查找的变量。

任何帮助表示赞赏。

谢谢

布雷克

【问题讨论】:

    标签: github-api


    【解决方案1】:

    好的,所以我联系了 github 技术支持,他们非常快速且乐于助人。

    • 发布时自动生成的 tarballs/zipballs 不会被跟踪下载周期数,即使它们在网站上被列为资产。它们显然是动态生成的。
    • 您可以通过生成发布、导航到它并选择右上角的“编辑发布”按钮来将二进制文件添加到您的发布中。向下滚动到“附加二进制文件...”并添加一个文件。

    现在要跟踪资产,所以旧的失败查询:

    curl https://api.github.com/repos/test10010000/countMyReleases/releases/tags/v1.0
    

    产生:

    curl https://api.github.com/repos/test10010000/countMyReleases/releases/tags/v1.0
    {
      "url": "https://api.github.com/repos/test10010000/countMyReleases/releases/34859773",
      "assets_url": "https://api.github.com/repos/test10010000/countMyReleases/releases/34859773/assets",
      "upload_url": "https://uploads.github.com/repos/test10010000/countMyReleases/releases/34859773/assets{?name,label}",
      "html_url": "https://github.com/test10010000/countMyReleases/releases/tag/v1.0",
      "id": 34859773,
      "author": {
        "login": "breckbaldwin",
        "id": 6377185,
        "node_id": "MDQ6VXNlcjYzNzcxODU=",
        "avatar_url": "https://avatars2.githubusercontent.com/u/6377185?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/breckbaldwin",
        "html_url": "https://github.com/breckbaldwin",
        "followers_url": "https://api.github.com/users/breckbaldwin/followers",
        "following_url": "https://api.github.com/users/breckbaldwin/following{/other_user}",
        "gists_url": "https://api.github.com/users/breckbaldwin/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/breckbaldwin/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/breckbaldwin/subscriptions",
        "organizations_url": "https://api.github.com/users/breckbaldwin/orgs",
        "repos_url": "https://api.github.com/users/breckbaldwin/repos",
        "events_url": "https://api.github.com/users/breckbaldwin/events{/privacy}",
        "received_events_url": "https://api.github.com/users/breckbaldwin/received_events",
        "type": "User",
        "site_admin": false
      },
      "node_id": "MDc6UmVsZWFzZTM0ODU5Nzcz",
      "tag_name": "v1.0",
      "target_commitish": "main",
      "name": "Test Count Downloads",
      "draft": false,
      "prerelease": false,
      "created_at": "2020-12-04T23:28:39Z",
      "published_at": "2020-12-04T23:29:18Z",
      "assets": [
        {
          "url": "https://api.github.com/repos/test10010000/countMyReleases/releases/assets/29251257",
          "id": 29251257,
          "node_id": "MDEyOlJlbGVhc2VBc3NldDI5MjUxMjU3",
          "name": "docs.txt.gz",
          "label": null,
          "uploader": {
            "login": "breckbaldwin",
            "id": 6377185,
            "node_id": "MDQ6VXNlcjYzNzcxODU=",
            "avatar_url": "https://avatars2.githubusercontent.com/u/6377185?v=4",
            "gravatar_id": "",
            "url": "https://api.github.com/users/breckbaldwin",
            "html_url": "https://github.com/breckbaldwin",
            "followers_url": "https://api.github.com/users/breckbaldwin/followers",
            "following_url": "https://api.github.com/users/breckbaldwin/following{/other_user}",
            "gists_url": "https://api.github.com/users/breckbaldwin/gists{/gist_id}",
            "starred_url": "https://api.github.com/users/breckbaldwin/starred{/owner}{/repo}",
            "subscriptions_url": "https://api.github.com/users/breckbaldwin/subscriptions",
            "organizations_url": "https://api.github.com/users/breckbaldwin/orgs",
            "repos_url": "https://api.github.com/users/breckbaldwin/repos",
            "events_url": "https://api.github.com/users/breckbaldwin/events{/privacy}",
            "received_events_url": "https://api.github.com/users/breckbaldwin/received_events",
            "type": "User",
            "site_admin": false
          },
          "content_type": "application/x-gzip",
          "state": "uploaded",
          "size": 56,
          "download_count": 2,
          "created_at": "2020-12-07T13:48:49Z",
          "updated_at": "2020-12-07T13:48:50Z",
          "browser_download_url": "https://github.com/test10010000/countMyReleases/releases/download/v1.0/docs.txt.gz"
        },
        {
          "url": "https://api.github.com/repos/test10010000/countMyReleases/releases/assets/29252040",
          "id": 29252040,
          "node_id": "MDEyOlJlbGVhc2VBc3NldDI5MjUyMDQw",
          "name": "Figure.1.pdf",
          "label": null,
          "uploader": {
            "login": "breckbaldwin",
            "id": 6377185,
            "node_id": "MDQ6VXNlcjYzNzcxODU=",
            "avatar_url": "https://avatars2.githubusercontent.com/u/6377185?v=4",
            "gravatar_id": "",
            "url": "https://api.github.com/users/breckbaldwin",
            "html_url": "https://github.com/breckbaldwin",
            "followers_url": "https://api.github.com/users/breckbaldwin/followers",
            "following_url": "https://api.github.com/users/breckbaldwin/following{/other_user}",
            "gists_url": "https://api.github.com/users/breckbaldwin/gists{/gist_id}",
            "starred_url": "https://api.github.com/users/breckbaldwin/starred{/owner}{/repo}",
            "subscriptions_url": "https://api.github.com/users/breckbaldwin/subscriptions",
            "organizations_url": "https://api.github.com/users/breckbaldwin/orgs",
            "repos_url": "https://api.github.com/users/breckbaldwin/repos",
            "events_url": "https://api.github.com/users/breckbaldwin/events{/privacy}",
            "received_events_url": "https://api.github.com/users/breckbaldwin/received_events",
            "type": "User",
            "site_admin": false
          },
          "content_type": "application/pdf",
          "state": "uploaded",
          "size": 223221,
          "download_count": 1,
          "created_at": "2020-12-07T14:08:23Z",
          "updated_at": "2020-12-07T14:08:23Z",
          "browser_download_url": "https://github.com/test10010000/countMyReleases/releases/download/v1.0/Figure.1.pdf"
        }
      ],
      "tarball_url": "https://api.github.com/repos/test10010000/countMyReleases/tarball/v1.0",
      "zipball_url": "https://api.github.com/repos/test10010000/countMyReleases/zipball/v1.0",
      "body": ""
    }
    

    我里面有 gzip 和 pdf。

    我把它作为练习留给读者如何将这种能力破解成有用的东西。

    布雷克

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-29
      • 2017-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多