几步搞定Github授权登录

本文主要介绍使用Github授权登录
如有需要,可以参考
如有帮助,不忘 点赞

1) 首先我们需要登录github,然后在setting中创建一个OAuth Apps 产生client_id和client_secret, 如图:

![在这里插入图片描述](https://img-blog.csdnimg.cn/2019080808324657.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzI4NzIzOQ==,size_16,color_FFFFFF,t_70
这样我们就创建好了一个Oauth App,点击进去会看到自己的client_idcilent_secret在这里插入图片描述

2)然后在网站的登录链接我们需要设置为https://github.com/login/oauth/authorize?client_id=?&redirect_uri=?&scope=?&state=?
  • client_id:是我们申请得到的
  • redirect_uri:我们自定义的跳转地址,是我们用来向git发出请求,然后git会带着"code"和"state"跳转到我们填写的重定向地址
  • scope:一般填 user
  • state:填1 便可
3)然后我们在redirect_uri的callback中接收到code和state , 继续访问https://github.com/login/oauth/access_token?client_id=?&client_secret=?&code=?&redirect_uri=?&state=?

带上参数(client_id,client_secret,code,redirect_uri,state)

4)访问成功后,git会返回一个accessToken给我们 , 这个时候我们可以通过访问http://api.github.com/user?access_token=?" , 获取一个user 的信息 , 然后我们想要什么信息直接获取便可.

猜你喜欢

转载自blog.csdn.net/weixin_43287239/article/details/98842874