Coder Social home page Coder Social logo

wechat-oauth's Introduction

wechat-oauth

微信公共平台OAuth接口消息接口服务中间件与API SDK

模块状态

  • NPM version
  • Build Status
  • Dependencies Status
  • Coverage Status

功能列表

  • OAuth授权
  • 获取基本信息

OAuth2.0网页授权,使用此接口须通过微信认证,如果用户在微信中(Web微信除外)访问公众号的第三方网页,公众号开发者可以通过此接口获取当前用户基本信息(包括昵称、性别、城市、国家)。详见:官方文档

详细参见API文档

Installation

$ npm install wechat-oauth

Usage

初始化

引入OAuth并实例化

var OAuth = require('wechat-oauth');
var client = new OAuth('your appid', 'your secret');

以上即可满足单进程使用。 当多进程时,token需要全局维护,以下为保存token的接口。

var oauthApi = new OAuth('appid', 'secret', function (openid, callback) {
  // 传入一个根据openid获取对应的全局token的方法
  // 在getUser时会通过该方法来获取token
  fs.readFile(openid +':access_token.txt', 'utf8', function (err, txt) {
    if (err) {return callback(err);}
    callback(null, JSON.parse(txt));
  });
}, function (openid, token, callback) {
  // 请将token存储到全局,跨进程、跨机器级别的全局,比如写到数据库、redis等
  // 这样才能在cluster模式及多机情况下使用,以下为写入到文件的示例
  // 持久化时请注意,每个openid都对应一个唯一的token!
  fs.writeFile(openid + ':access_token.txt', JSON.stringify(token), callback);
});

引导用户

生成引导用户点击的URL。

var url = client.getAuthorizeURL('redirectUrl', 'state', 'scope');

如果是PC上的网页,请使用以下方式生成

var url = client.getAuthorizeURLForWebsite('redirectUrl');

获取Openid和AccessToken

用户点击上步生成的URL后会被重定向到上步设置的 redirectUrl,并且会带有code参数,我们可以使用这个code换取access_token和用户的openid

client.getAccessToken('code', function (err, result) {
  var accessToken = result.data.access_token;
  var openid = result.data.openid;
});

获取用户信息

如果我们生成引导用户点击的URL中scope参数值为snsapi_userinfo,接下来我们就可以使用openid换取用户详细信息(必须在getAccessToken方法执行完成之后)

client.getUser(openid, function (err, result) {
  var userInfo = result;
});

捐赠

如果您觉得Wechat OAuth对您有帮助,欢迎请作者一杯咖啡

捐赠wechat

或者

交流群

QQ群:157964097,使用疑问,开发,贡献代码请加群。

Contributors

感谢以下贡献者:

$ git summary

 project  : wechat-oauth
 repo age : 3 months
 active   : 3 days
 commits  : 9
 files    : 12
 authors  :
     8  Jackson Tian  88.9%
     1  Teng Fei      11.1%

License

The MIT license.

wechat-oauth's People

Contributors

jacksontian avatar kainy avatar terry-fei avatar w304807481 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.