原文我的博客:支持原文吧? 一文带你搞定github三方登录(web端)
前言:
作为程序员,大概率是有github账号的,而我的博客,属于技术类分享,so 博客接入三方登录首选肯定是github拉,github 一个大型在线男男社交平台~~~
github 三方登录流程
- 用户向github发起登录授权 -> github.com/login/oauth…
- 跳转到github授权页面,用户点击授权 将页面重定向到你填写的回调地址上并带上code,作为下一步请求的标识
- 在回调地址页面 向自己的服务器发起请求,带上code
- 后端用code 请求access_token -> github.com/login/oauth…
- 后端拿到token -> 用token请求用户信息 api.github.com/user
- 后端拿到用户信息进行自己的逻辑处理
废话不多说!开始接入吧
第一步 创建github OAuth app
用户 -> settings -> develop settings -> OAuth Apps -> new OAuth App
然后填好对应的信息
最后一个是回调地址。
为了调用接口,你还需要创建一个client secrets ,创建之后请记录下来,只会让你看一次
前端发起授权请求
https://github.com/login/oauth/authorize?client_id=申请的Client ID&scope=user:email
复制代码
scope
后面是要申请的信息内容 scope 对应文档
点击接受之后会跳转到github上填写的回调地址并且加上code例如:text.com/callback?co…
后端获取用户信息
一、根据前端code 获取accsess_token
后端我使用go写的,使用github授权后跳转页面带的code 获取access_token这里贴出一点代码
获取接口:
github.com/login/oauth…
获取参数:client_id
、client_secret
、code
param := make(map[string]string)
param["client_id"]="你的client_id"
param["client_secret"]="生成的client_secret"
param["code"]= code //从前端获取code
var tokenResult response.AccessTokenResult
err := utils.FetchPostForm("https://github.com/login/oauth/access_token",param,&tokenResult,nil)
if err != nil {
utils.Log.Error(err)
return err
}
utils.Log.Info(fmt.Sprintf("获取github验证token %s",tokenResult))
复制代码
获取到正确的token,这里注意,要获取json header 要这样设置 Accept: application/json
{"access_token":"gho_16C7e42F292c6912E7710c838347Ae178B4a", "scope":"repo,gist", "token_type":"bearer"}
复制代码
注意:github 访问有点问题,我使用了翻墙软件,但是后端程序在本地运行仍然不能访问github的接口,我使用了配置本地host的方式进行访问
这里给出配置host的方式:
mac配置host:
终端运行一下命令
sudo vim /private/etc/hosts
复制代码
输入如密码后修改host
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
13.250.177.223 github.com
复制代码
二、使用access_token获取用户信息
获取用户信息接口地址:api.github.com/user
token 放在header里
Authorization: token OAUTH-TOKEN
OAUTH-TOKEN 是上一步获取的token
后端代码
header := make(map[string]string)
header["Authorization"] = "token "+tokenResult.AccessToken
var user response.GithubUserInfo
err = utils.FetchGet("https://api.github.com/user",&user,header)
if err != nil {
utils.Log.Error(err)
return err
}
utils.Log.Info(fmt.Sprintf("获取github 用户信息 %s",user))
复制代码
获取到数据信息:
{
"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,
"name": "monalisa octocat",
"company": "GitHub",
"blog": "https://github.com/blog",
"location": "San Francisco",
"email": "octocat@github.com",
"hireable": false,
"bio": "There once was...",
"twitter_username": "monatheoctocat",
"public_repos": 2,
"public_gists": 1,
"followers": 20,
"following": 0,
"created_at": "2008-01-14T04:33:35Z",
"updated_at": "2008-01-14T04:33:35Z"
}
复制代码
后端获取到数据接下来就是注册流程。