📅  最后修改于: 2023-12-03 15:15:20.281000             🧑  作者: Mango
Github 用户 API 提供了访问 Github 用户数据的接口,包括用户基本信息、仓库列表、关注列表等。本文将介绍 Github 用户 API 的应用场景、使用方法以及注意事项。
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
:仓库 IDname
:仓库名称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
:用户 IDavatar_url
:头像地址url
:用户地址User-Agent
字段,用来标识请求的来源,例如 Mozilla/5.0 (Windows NT 10.0; Win64; x64)...
。Authorization
字段,用来进行身份认证,例如 Bearer YOUR_PERSONAL_ACCESS_TOKEN
。Accept: application/json
,否则会返回默认的 HTML 格式。