npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-weixin-oauth

v0.3.0

Published

>

Downloads

227

Readme

node-weixin-oauth NPM version Build Status Dependency Status Coverage percentage

微信 Oauth 模块,用于验证微信用户,并且获取用户信息

说明

这个项目是 (node-weixin-api 或者 node-weixin-express) 的一个子项目。

作用是完成用户的Oauth验证。

微信Oauth模块与Auth模块在node-weixin里是两个不同的模块。

  1. Oauth主要用于用户身份的验证。
  2. Auth主要用于应用服务器的验证。即运行着auth模块的用户服务器被腾讯官方的微信服务器所验证。

Oauth的实现主要分成以下几个部分。

  1. 重新向到验证服务,并且提交返回URL
  2. 在验证服务器输入用户登录信息(在微信中,由于用户已经登录,所以不需要这个过程)
  3. 成功则返回到提交的URL,并提交code给用户的应用服务器。
  4. 用户的应用服务器根据code,结合自己的appid, appsecret向微信服务器请求acess token, refresh token, openid等

交流QQ群: 39287176

注:

node-weixin-express是基于node-weixin-*的服务器端参考实现。

node-weixin-api是基于node-weixin-*的API接口SDK。

它们都是由下列子项目组合而成:

  1. node-weixin-config 用于微信配置信息的校验

  2. node-weixin-auth 用于与微信服务器握手检验

  3. node-weixin-util 一些常用的微信请求,加密,解密,检验的功能与处理

  4. node-weixin-request 微信的各类服务的HTTP请求的抽象集合

  5. node-weixin-oauth 微信OAuth相关的操作

  6. node-weixin-pay 微信支付的服务器接口

  7. node-weixin-jssdk 微信JSSDK相关的服务器接口

  8. node-weixin-menu 微信菜单相关的操作与命令

  9. node-weixin-user 微信用户API

  10. node-weixin-media 微信多媒体API

  11. node-weixin-qrcode 微信二维码API

安装

$ npm install --save node-weixin-oauth

使用

1、得到oauth对象

var nwo = require('node-weixin-oauth');

2、创建用户通过微信可访问的URL

  • state是随意表示当前程序状态的值
  • userInfo: 0 表示最少的基本信息, 1表示获取更多用户信息
  • 创建好URL后,需要将用户引导到创建的地址进行校验
var url = nwo.createURL(appId, redirectUri, state, userInfo)
res.redirect(url);

3、在重定向函数里处理调用信息 在校验用户成功后,微信会将用户带回到redirectUri指定的地址进行下一步操作 在redirectUri里需要使用success来对返回的code进行校验,并获取以下三样数据:

  1. 微信服务器的access token
  2. 微信服务器的refresh token
  3. 用户的openid 可以通过nwo.session访问,也可以通过返回的body访问。 nwo.session里的数据结构如下:
        {
        oauth.session = {
          openId: json.openid,
          accessToken: json.access_token,
          refreshToken: json.refresh_token
        };
  调用如下:
  • 其中
    • app:是node-weixin-config通过app.init校验的数据
    • code:服务器返回校验数据
nwo.success(app, code, function(error, body) {
  if (!error) {
    var accessToken = body.acess_token;
    var refreshToken = body.refresh_token;
  }
});

4、获取用户信息(可选) 当scope为1时,我们还可以进一步的获取用户信息

nwo.profile(openId, accessToken, function(error, body) {
});

5、刷新access token(可选)

nwo.refresh(appId, refreshToken, function(error, body) {
});

6、检验token有效性(可选)

nwo.validate(openid, refreshToken, function(error, body) {
});

实际的调用过程参考node-weixin-express

License

Apache-2.0 © calidion