例图
1 注册 github 账号
2 Settings -> Developer applications -> new OAuth App
Authorization callback URL
--后端回调url, github 会调用这个url 将code
发送给你,你需要获取这个code
之后,再去获取 access_token
,再通过 access_token
获取用户信息
3 你已创建好 OAuth Apps 我们开始使用
3.1 你的网站上需要一个登录按钮1
2
3
4
5
6
7
8<a class="btn btn-light btn-sm" href="https://github.com/login/oauth/authorize?client_id=xxx&state=xxx&redirect_uri=xxx">
<svg height="14" class="octicon octicon-mark-github" viewBox="0 0 16 16" version="1.1" width="14"
aria-hidden="true">
<path fill-rule="evenodd"
d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0 0 16 8c0-4.42-3.58-8-8-8z"></path>
</svg>
<span style="font-size: 16px;">登录</span>
</a>
注意他的 href 中client_id 从你创建的 OAuth Apps 找到,redirect_uri 即 Authorization callback URL(注: 要使用公网ip地址)
3.2 上一步你拿到code,现在获取 access_token
"https://github.com/login/oauth/access_token?client_id=xxx&client_secret=xxx&code=" + code + "&redirect_uri=xxx";
注:每个参数都是必须的具体参考文档 https://developer.github.com/v3/
client_id
和 client_secret
从应用中拿到即可 redirect_uri
即上一步中的一样即可
3.3获取用户信息1
2
3
4
5
6
7
8
9let access_token_url = "https://api.github.com/?access_token=" + access_token;
let headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3571.0 Safari/537.36'};
// GET
request(access_token_url, {headers}, function (err, rs, userInfo) {
if (err) {
logger.error(err)
}
logger.info(userInfo);
});
注:* 请求头里必须有 User-Agent
* 必须用 get 请求
结果1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33{
"login": "yuluhuang",
"id": 5743499,
"node_id": "MDQ6VXNlcjU3NDM0OTk=",
"avatar_url": "https://avatars1.githubusercontent.com/u/5743499?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/yuluhuang",
"html_url": "https://github.com/yuluhuang",
"followers_url": "https://api.github.com/users/yuluhuang/followers",
"following_url": "https://api.github.com/users/yuluhuang/following{/other_user}",
"gists_url": "https://api.github.com/users/yuluhuang/gists{/gist_id}",
"starred_url": "https://api.github.com/users/yuluhuang/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/yuluhuang/subscriptions",
"organizations_url": "https://api.github.com/users/yuluhuang/orgs",
"repos_url": "https://api.github.com/users/yuluhuang/repos",
"events_url": "https://api.github.com/users/yuluhuang/events{/privacy}",
"received_events_url": "https://api.github.com/users/yuluhuang/received_events",
"type": "User",
"site_admin": false,
"name": "luhuang",
"company": null,
"blog": "https://yuluhuang.com",
"location": null,
"email": "504367857@qq.com",
"hireable": null,
"bio": null,
"public_repos": 30,
"public_gists": 4,
"followers": 5,
"following": 5,
"created_at": "2013-10-22T04:31:11Z",
"updated_at": "2018-09-30T03:25:01Z"
}