优先考虑 GitHub Apps 这里讲解 OAuth Apps
创建 GitHub OAuth Application
1 | 应用程序名称:顾名思义 |
创建完之后记住Client Id和Client secrets,如:
1 | Client Id: Ov23liTTSlBvlKogMGEG |
应用程序代码部分
认证授权步骤
前端页面携带 Client Id 和 Redirect Uri 访问 https://github.com/login/oauth/authorize
1
GET https://github.com/login/oauth/authorize?client_id=${clientId}&redirect_uri=${redirectUri}
认证和授权完毕之后GitHub会将页面重定向到 Redirect Uri,并且会携带code值,code用来交换access_token
1
http://localhost/callback?code=e94944d265321bf5654d
后端应用程序创建一个WEB API接口,使用code交换access_token
1
2
3
4
5
6
7
8
9
10
11# 在接口中调用面这个 HTTP API 用来换取 access_token
POST https://github.com/login/oauth/access_token
Content-Type: application/json
{
"client_id": "Ov23liTTSlBvlKogMGEG",
"client_secret": "*****8bfc50b7",
"code": "e94944d265321bf5654d",
"redirect_uri": "http://localhost/callback",
"state": "随机字符串"
}
注意 Client secrets 一定不要存储到客户端当中,泄漏之后会出现安全问题。