node获取微信授权拿到openid

需要了解的网站

  1.微信授权。

先说一下流程(一张图代替所有):

流程步骤:

1.用户同意,获取code。

2.通过code获取网页授权access_token.

3.获取用户信息。

开始搞事情:

这是我的路由结构。

const Koa = require('koa')
const app = new Koa()
const path = require('path')
const route = require('koa-route');
const static = require('koa-static');
const keyBody = require('koa-body')// routes
const { oauth } = require('./routes/accredit/oauth');
const { token } = require('./routes/accredit/token');
const rootPath = path.join(__dirname + '/View')
const _static = static(rootPath)// 中间件
const logger = async(ctx, next) => {const rt_start = Date.now()await next()const rt_end = Date.now()ctx.set('X-Response-Time', `${rt_end - rt_start}ms`);console.log(ctx.request.method, ctx.url, `${rt_end - rt_start}ms`)
}app.use(_static) // 静态资源
app.use(keyBody()) // req body数据获取 (非参数序列化)
app.use(logger) // 日志// page route
app.use(route.get('/oauth', oauth)); //授权
app.use(route.get('/token', token)); //获取openid
app.listen(8088, (err) => {if (err) { console.error(err) }console.log('Listening At:', 8088)
}

1.在APP中访问oauth获取code

var config = require('./../config');
var request = require('request');
/* 微信网页授权 */
const oauth = async(ctx, next) => {const { request: req, response: res } = ctx;             var AppID = config.AppID;var AppSecret = config.AppSecret;// 第一步:用户同意授权,获取codevar Router = 'jy';// 这是编码后的地址var return_uri = config.return_uri + Router;var scope = 'snsapi_base';// snsapi_userinfo可以获取用户信息与token与openid// snsapi_base只能获取到token与openidres.redirect('https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + AppID + '&redirect_uri=' + return_uri + '&response_type=code&scope=' + scope + '&state=123456#wechat_redirect');
}
module.exports = { oauth };

1.01(config.js)//写好配置参数

(1):AppID,

    (2):AppSecret。

2.在客户端访问 tocken,tongguo code获取access_tocken

var config = require('./../config');
var request = require('request');
var axios = require('axios')
const token = async(ctx, next) => {const { request: req, response: res } = ctxvar code = req.header.referer.match(new RegExp("[\?\&]" + 'code' + "=([^\&]+)", "i"))[1];var AppID = config.AppID;var AppSecret = config.AppSecret;var result = await request.get({url: 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' + AppID + '&secret=' + AppSecret + '&code=' + code + '&grant_type=authorization_code',},function(error, response, body) {if (response.statusCode == 200) {// 第三步:拉取用户信息(需scope为 snsapi_userinfo)// console.log(JSON.parse(body));var data = JSON.parse(body);var access_token = data.access_token;var openid = data.openid;} else {console.log(response.statusCode);}});ctx.type = 'json';ctx.body = result;
}module.exports = { token }

因为我这里只需要获取到openid即可,所以在这里就已经返回result。

在这里再次感谢在人生路上帮助我的人!谢谢你们。

如果以上代码对您有用,欢迎打赏!如有错误的地方也请您留言指出。

      (支付宝)                  (微信)

转载于:https://www.cnblogs.com/jimmy1293/p/8969057.html

node 微信授权 获取openid相关推荐

  1. 微信授权获取openID等信息,这里简化记录一下

    微信授权获取openID等信息 微信测试平台连接:https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login 授权操作必须用外网(推荐 ...

  2. Weixin4j微信开发网页授权获取openid案例

    前言 weixin4j网页静默授权获取openid案例 **说明:**微信网页授权基础知识请参考官方文档. 静默授权获取OpenId 本 示例基于weixin4j开发,weixin4j是Java微信开 ...

  3. 微信授权获取code请求openID,js+java

    作者:LoveEmperor-王子様 微信网页获取用户授权 请见另一版: https://blog.csdn.net/qq_31424825/article/details/80272364 背景 * ...

  4. H5在微信中获取openid

    H5在微信中获取openid 为什么要获取openid openId是用户在当前公众号下的唯一标识('身份证').在微信中进行微信分享.支付等操作时需要用户的openid.H5要在微信中获取到用户op ...

  5. html获取微信code,微信授权获取code(微信支付)

    微信授权获取code(微信支付) 2019-03-02 编程之家 https://www.jb51.cc 编程之家收集整理的这篇文章主要介绍了微信授权获取code(微信支付),编程之家小编觉得挺不错的 ...

  6. 微信授权获取用户的openid和支付宝授权获取用户的userid

    为什么80%的码农都做不了架构师?>>>    当一请求一个链接或者是扫描二维码时,会请求后台方法,当然对于微信和支付宝来说,大多数时候是扫 码 一.首先说微信: 1.首先会判断请求 ...

  7. 微信授权获取用户openid前端实现

    近来,倒霉的后台跟我说让我拿个openid做微信支付使用,寻思很简单,开始干活. 首先引导用户打开如下链接,只需要将appid修改为自己的就可以,redirect_url写你的重定向url https ...

  8. php 40163,微信支付授权获取 openId {errcode:40163,errmsg:code been used, hints: [ req_id: scqL1a02482017...

    微信支付授权获取用户openId时候,返回40163,原因: 微信支付获取用户openid时,报出错误.原因是同时配置了http和https,微信网页oauth认证通知了两次 删除apache配置文件 ...

  9. 微信公众平台-测试号网页授权-获取openid方法

    文章目录 1.创建自己的测试号 2.测试号管理信息填写(注意仔细一步步对照) 3.手动获取openid 4.使用SDK获取openid 1.创建自己的测试号 通过申请链接:https://mp.wei ...

最新文章

  1. 卷积后feature map尺寸计算公式
  2. python字符串压缩字_gzip如何在Python中压缩字符串?
  3. 黑客这样使用python发邮件
  4. LeetCode 478. 在圆内随机生成点(概率)
  5. php mysql odbc_javascript连接mysql与php通过odbc连接任意数据库的实例
  6. springboot 之Spring Web Mvc Framework
  7. Wijmo 更优美的jQuery UI部件集:客户端更改C1GridView数据源
  8. sqlserver 指定的网络名不再可用_50个比较实用的SQL Server查询语句(1)
  9. Python之队列和数据库
  10. 浏览器后退不刷新页面
  11. CCF——游戏(2017-12)
  12. 基于 React.js + redux + bootstrap 的 RubyChina 示例
  13. Android 银行账号
  14. 学习笔记-WinRM
  15. 296 最佳的碰头地点
  16. php 对账单系统,PHP实现微信对账单处理
  17. SolidEdge 工程图中如何控制是否显示爆炸图组装线
  18. 登录邮箱手动连接服务器,outlook2016不能连接exchange2010,自动或手动均不能连接服务器...
  19. SCI论文写作(一) | SCI论文的文献综述(Literature Review)部分
  20. Promise学习-手写一个promise

热门文章

  1. windows 技巧篇-查看文件夹被那个进程占用,文件夹占用解除方法
  2. Python 语法问题-module ‘pip._internal‘ has no attribute ‘pep425tags‘. 原因及解决办法,32位、64位查看pip支持万能方法
  3. CTFshow 命令执行 web54
  4. Hermite曲线与Bezier曲线的关系
  5. 画单自由度系统传递函数(实频,虚频,幅频,相位,导纳)
  6. [YTU ]_2736指针练习--输出最大值
  7. Python中函数的参数传递方式
  8. python 计算当月天数_告诉你怎么用Python进行企业营运分析!盈利这么多?
  9. Python相关系数矩阵热力图(二)
  10. Numpy.argsort()(Python)