热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

node第三方登陆github(express)

1.新建授权app(OAuth—Apps)创建成功以后:ClientID(key)和ClientSecret(密钥)是需要用到的2.使用的

1.新建授权app(OAuth—Apps)

创建成功以后:Client ID(key)和 Client Secret(密钥)是需要用到的

 

 

2.使用的node框架:express

下载node-fetch

 

yarn add node-fetch

 

 

npm i node-fetch -s

 

 

新建login路由:

scope:授权服务

state:回调参数,

 

router.get("/login", function (req, resp) {let path = "https://github.com/login/oauth/authorize";path += '?client_id=';path += '&state=state';path += '&scope=user:email';resp.redirect(path);
})


直接跳转到github第三方登陆页面,授权成功会回调在OAuth Apps设置的回调url

 

 

新建github路由

 

router.get("/github", function (req, resp) {let path = 'https://github.com/login/oauth/access_token';const params = {client_id: '',client_secret: '',code: req.query.code}fetch(path, {method: 'POST',headers: {'Content-Type': 'application/json'},body: JSON.stringify(params)}).then(function (res) {return res.text();}).then(function (body) {const args = body.split('&');let arg = args[0].split('=');const access_token = arg[1];console.log(body);console.log(access_token);return access_token;}).then(async (token) => {const url = ' https://api.github.com/user?access_token=' + token;await fetch(url).then(res => {return res.json();}).then(res => {console.log(res);resp.send(res);})})
})

 

 

 


 

 

 


推荐阅读
author-avatar
初2新生_349
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有