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

weixin-open-oauth

v1.0.0

Published

针对微信公众号第三方开发者,封装了授权流程中的主要api, 详情见微信开放平台文档[授权流程说明](https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1453779503&token=e7e06f30f4625f3274a06dd29b07d76f8aa00da7&lang=zh_CN)。

Downloads

5

Readme

针对微信公众号第三方开发者,封装了授权流程中的主要api, 详情见微信开放平台文档授权流程说明

同时,封装了代公众号发起网页授权的相关接口,详情见代公众号发起网页授权

该模块主要参考了node-webotwechat-oauthwechat-api

使用该插件完成授权之后,可以利用open-wechat-api去执行小程序的各项主动调用api

功能列表

基础授权

  • 获取第三方平台component_access_token
  • 获取预授权码pre_auth_code
  • 获取公众号的接口调用凭据和授权信息
  • 刷新授权公众号的接口调用凭据
  • 获取授权公众号账号基本信息
  • 获取授权方的选项设置信息
  • 设置授权方的选项信息

网页授权

  • 获取授权页面的URL地址
  • 通过code换取access_token
  • 刷新access_token
  • 通过access_token获取用户基本信息

安装

$ npm install weixin-open-oauth

使用方法(基础授权)

使用该插件需要自己缓存微信第三方平台所需的component_verify_ticket, component_access_token,建议存储在redis中,参考以下方法创建授权对象实例:

var WXAuth = require('weixin-open-oauth');

/*
 * 获取全局component_verify_ticket的方法
 * 从redis缓存中读取
 */
var getVerifyTicket = async function() {
  let ticket = await redisClient.get('component_verify_ticket');
  if(!ticket) throw new Error('ticket is null')
    return ticket
};

/*
 * 获取全局component_access_token的方法
 * 从redis缓存中读取
 */
var getComponentToken = async function(callback) {
  let token = await redisClient.get('component_access_token');
  return JSON.parse(token)
};

/*
 * 保存component_access_token到redis中
 */
var saveComponentToken = async function(token) {
  return await redisClient.setex('component_access_token', 7000, JSON.stringify(token));
};

var wxauth = new WXAuth(appid, appsecret, getVerifyTicket, getComponentToken, saveComponentToken);

获取第三方平台component_access_token

wxauth.getLatestComponentToken();

获取预授权码pre_auth_code

wxauth.getPreAuthCode();

获取公众号的接口调用凭据和授权信息

// auth_code 授权完成后微信返回的授权码
wxauth.getAuthToken(auth_code);

刷新授权公众号的接口调用凭据

// authorizer_appid 授权公众号的appid
// authorizer_refresh_token 从微信获取的公众号刷新token,存储在db中
wxauth.refreshAuthToken(authorizer_appid, authorizer_refresh_token);

获取授权公众号账号基本信息

// authorizer_appid 授权公众号的appid
wxauth.getAuthInfo(authorizer_appid);

获取授权方的选项设置信息

// authorizer_appid 授权公众号的appid
// option_name 选项名
wxauth.getAuthOption(authorizer_appid, option_name);

设置授权方的选项信息

// authorizer_appid 授权公众号的appid
// option_name 选项名
// option_value选项值
wxauth.setAuthOption(authorizer_appid, option_name, option_value);

网页授权

获取授权页面的URL地址

// appid 授权公众号的appid
// redirect 授权后要跳转的地址
// state 开发者可提供的数据
// scope 作用范围,值为snsapi_userinfo和snsapi_base,前者用于弹出,后者用于跳转
var oauthUrl = wxauth.getOAuthURL(appid, redirect, state, scope);

通过code换取access_token

// appid 授权公众号的appid
// code 授权获取到的code
// callback 回调函数
wxauth.getOAuthAccessToken(appid, code);

刷新access_token

// appid 授权公众号的appid
// refresh_token 授权刷新token
// callback 回调函数
wxauth.refreshOAuthAccessToken(appid, refresh_token);

通过access_token获取用户基本信息

// openid 授权用户的唯一标识
// access_token 网页授权接口调用凭证
// lang 语言版本,zh_CN 简体,zh_TW 繁体,en 英语
// callback 回调函数
wxauth.getUserInfo(openid, access_token, lang);