📜  github 用户 api (1)

📅  最后修改于: 2023-12-03 15:15:20.281000             🧑  作者: Mango

Github 用户 API

Github 用户 API 提供了访问 Github 用户数据的接口,包括用户基本信息、仓库列表、关注列表等。本文将介绍 Github 用户 API 的应用场景、使用方法以及注意事项。

应用场景
  • 查看 Github 用户的基本信息,例如用户名、邮箱、头像等。
  • 获取 Github 用户的仓库列表以及对应仓库的相关信息。
  • 查看 Github 用户的关注列表以及被关注列表。
  • 获取 Github 用户的 followers 和 following 数量。
  • 查询 Github 用户的活跃度、贡献度等。
使用方法

Github 用户 API 的请求地址为 https://api.github.com/users/{username},其中 {username} 是 Github 用户名。需要注意的是,Github API 有访问频率限制,每小时最多请求 60 次,若超过限制则会被禁止访问一段时间。

查询用户基本信息

要查询 Github 用户的基本信息,只需向上述请求地址发起 GET 请求即可。请求成功后,会返回如下的 Json 格式数据:

{
  "login": "octocat",
  "id": 1,
  "node_id": "MDQ6VXNlcjE=",
  "avatar_url": "https://avatars.githubusercontent.com/u/1?v=4",
  "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,
  "name": "The Octocat",
  "company": "GitHub",
  "blog": "https://github.com/blog",
  "location": "San Francisco",
  "email": null,
  "hireable": null,
  "bio": null,
  "twitter_username": null,
  "public_repos": 237,
  "public_gists": 473,
  "followers": 11225,
  "following": 9,
  "created_at": "2008-01-14T04:33:35Z",
  "updated_at": "2021-08-03T18:27:30Z"
}

其中,比较常用的属性有:

  • login:用户名
  • name:姓名
  • avatar_url:头像地址
  • email:邮箱
  • public_repos:公开仓库数量
  • followers:关注者数量
  • following:被关注者数量
  • created_at:账户创建时间
查询用户仓库列表

要查询 Github 用户的仓库列表,只需向上述请求地址加上 /repos 路径即可。请求成功后,会返回如下的 Json 格式数据:

[
  {
    "id": 132935648,
    "node_id": "MDEwOlJlcG9zaXRvcnkxMzI5MzU2NDg=",
    "name": "HelloWorld",
    "full_name": "octocat/HelloWorld",
    "private": false,
    "owner": {
      "login": "octocat",
      "id": 1,
      "node_id": "MDQ6VXNlcjE=",
      "avatar_url": "https://avatars.githubusercontent.com/u/1?v=4",
      "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
    },
    "html_url": "https://github.com/octocat/HelloWorld",
    "description": "This your first repo!",
    "fork": false,
    "url": "https://api.github.com/repos/octocat/HelloWorld",
    "forks_url": "https://api.github.com/repos/octocat/HelloWorld/forks",
    "keys_url": "https://api.github.com/repos/octocat/HelloWorld/keys{/key_id}",
    "collaborators_url": "https://api.github.com/repos/octocat/HelloWorld/collaborators{/collaborator}",
    "teams_url": "https://api.github.com/repos/octocat/HelloWorld/teams",
    "hooks_url": "https://api.github.com/repos/octocat/HelloWorld/hooks",
    "issue_events_url": "https://api.github.com/repos/octocat/HelloWorld/issues/events{/number}",
    "events_url": "https://api.github.com/repos/octocat/HelloWorld/events",
    "assignees_url": "https://api.github.com/repos/octocat/HelloWorld/assignees{/user}",
    "branches_url": "https://api.github.com/repos/octocat/HelloWorld/branches{/branch}",
    "tags_url": "https://api.github.com/repos/octocat/HelloWorld/tags",
    "blobs_url": "https://api.github.com/repos/octocat/HelloWorld/git/blobs{/sha}",
    "git_tags_url": "https://api.github.com/repos/octocat/HelloWorld/git/tags{/sha}",
    "git_refs_url": "https://api.github.com/repos/octocat/HelloWorld/git/refs{/sha}",
    "trees_url": "https://api.github.com/repos/octocat/HelloWorld/git/trees{/sha}",
    "statuses_url": "https://api.github.com/repos/octocat/HelloWorld/statuses/{sha}",
    "languages_url": "https://api.github.com/repos/octocat/HelloWorld/languages",
    "stargazers_url": "https://api.github.com/repos/octocat/HelloWorld/stargazers",
    "contributors_url": "https://api.github.com/repos/octocat/HelloWorld/contributors",
    "subscribers_url": "https://api.github.com/repos/octocat/HelloWorld/subscribers",
    "subscription_url": "https://api.github.com/repos/octocat/HelloWorld/subscription",
    "commits_url": "https://api.github.com/repos/octocat/HelloWorld/commits{/sha}",
    "git_commits_url": "https://api.github.com/repos/octocat/HelloWorld/git/commits{/sha}",
    "comments_url": "https://api.github.com/repos/octocat/HelloWorld/comments{/number}",
    "issue_comment_url": "https://api.github.com/repos/octocat/HelloWorld/issues/comments{/number}",
    "contents_url": "https://api.github.com/repos/octocat/HelloWorld/contents/{+path}",
    "compare_url": "https://api.github.com/repos/octocat/HelloWorld/compare/{base}...{head}",
    "merges_url": "https://api.github.com/repos/octocat/HelloWorld/merges",
    "archive_url": "https://api.github.com/repos/octocat/HelloWorld/{archive_format}{/ref}",
    "downloads_url": "https://api.github.com/repos/octocat/HelloWorld/downloads",
    "issues_url": "https://api.github.com/repos/octocat/HelloWorld/issues{/number}",
    "pulls_url": "https://api.github.com/repos/octocat/HelloWorld/pulls{/number}",
    "milestones_url": "https://api.github.com/repos/octocat/HelloWorld/milestones{/number}",
    "notifications_url": "https://api.github.com/repos/octocat/HelloWorld/notifications{?since,all,participating}",
    "labels_url": "https://api.github.com/repos/octocat/HelloWorld/labels{/name}",
    "releases_url": "https://api.github.com/repos/octocat/HelloWorld/releases{/id}",
    "deployments_url": "https://api.github.com/repos/octocat/HelloWorld/deployments",
    "created_at": "2018-05-15T15:20:18Z",
    "updated_at": "2021-09-20T09:42:13Z",
    "pushed_at": "2021-09-20T09:42:11Z",
    "git_url": "git://github.com/octocat/HelloWorld.git",
    "ssh_url": "git@github.com:octocat/HelloWorld.git",
    "clone_url": "https://github.com/octocat/HelloWorld.git",
    "svn_url": "https://github.com/octocat/HelloWorld",
    "homepage": "",
    "size": 47,
    "stargazers_count": 1563,
    "watchers_count": 1563,
    "language": null,
    "has_issues": true,
    "has_projects": true,
    "has_downloads": true,
    "has_wiki": true,
    "has_pages": false,
    "forks_count": 1121,
    "mirror_url": null,
    "archived": false,
    "disabled": false,
    "open_issues_count": 28,
    "license": null,
    "forks": 1121,
    "open_issues": 28,
    "watchers": 1563,
    "default_branch": "master",
    "temp_clone_token": null,
    "network_count": 1121,
    "subscribers_count": 42
  },
  // ...
]

其中,每个仓库数据的常用属性包括:

  • id:仓库 ID
  • name:仓库名称
  • full_name:仓库完整名称
  • description:仓库描述
  • url:仓库地址
  • stargazers_count:收藏数
  • forks_count:被 fork 数
  • updated_at:最后更新时间
查询用户关注列表

要查询 Github 用户的关注列表,只需向上述请求地址加上 /following 路径即可。请求成功后,会返回如下的 Json 格式数据:

[
  {
    "login": "turing",
    "id": 790394,
    "node_id": "MDQ6VXNlcjc5MDM5NA==",
    "avatar_url": "https://avatars.githubusercontent.com/u/790394?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/turing",
    "html_url": "https://github.com/turing",
    "followers_url": "https://api.github.com/users/turing/followers",
    "following_url": "https://api.github.com/users/turing/following{/other_user}",
    "gists_url": "https://api.github.com/users/turing/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/turing/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/turing/subscriptions",
    "organizations_url": "https://api.github.com/users/turing/orgs",
    "repos_url": "https://api.github.com/users/turing/repos",
    "events_url": "https://api.github.com/users/turing/events{/privacy}",
    "received_events_url": "https://api.github.com/users/turing/received_events",
    "type": "User",
    "site_admin": false
  },
  // ...
]

其中,每个关注用户的数据常用属性包括:

  • login:用户名
  • id:用户 ID
  • avatar_url:头像地址
  • url:用户地址
注意事项
  • 在请求 Github API 的时候,需要在请求头中加入 User-Agent 字段,用来标识请求的来源,例如 Mozilla/5.0 (Windows NT 10.0; Win64; x64)...
  • 在请求 Github API 的时候,需要在请求头中加入 Authorization 字段,用来进行身份认证,例如 Bearer YOUR_PERSONAL_ACCESS_TOKEN
  • Github API 的数据传输格式默认为 JSON 格式,需要在请求头中指定 Accept: application/json,否则会返回默认的 HTML 格式。
  • Github API 的请求次数有限制,每个用户每小时最多请求 60 次,需要合理使用。