Skip to content

Commit 2b3dab9

Browse files
committed
Init project
0 parents  commit 2b3dab9

File tree

10 files changed

+868
-0
lines changed

10 files changed

+868
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
example
3+
.DS_Store
4+
coverage

MIT-License

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Copyright (c) 2013 Jackson Tian
2+
http://weibo.com/shyvo
3+
4+
The MIT License
5+
6+
Permission is hereby granted, free of charge, to any person obtaining
7+
a copy of this software and associated documentation files (the
8+
"Software"), to deal in the Software without restriction, including
9+
without limitation the rights to use, copy, modify, merge, publish,
10+
distribute, sublicense, and/or sell copies of the Software, and to
11+
permit persons to whom the Software is furnished to do so, subject to
12+
the following conditions:
13+
14+
The above copyright notice and this permission notice shall be
15+
included in all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Makefile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
TESTS = test/*.js
2+
REPORTER = spec
3+
TIMEOUT = 20000
4+
ISTANBUL = ./node_modules/.bin/istanbul
5+
MOCHA = ./node_modules/mocha/bin/_mocha
6+
COVERALLS = ./node_modules/coveralls/bin/coveralls.js
7+
8+
test:
9+
@NODE_ENV=test $(MOCHA) -R $(REPORTER) -t $(TIMEOUT) \
10+
$(MOCHA_OPTS) \
11+
$(TESTS)
12+
13+
test-cov:
14+
@$(ISTANBUL) cover --report html $(MOCHA) -- -t $(TIMEOUT) -R spec $(TESTS)
15+
16+
test-coveralls:
17+
@$(ISTANBUL) cover --report lcovonly $(MOCHA) -- -t $(TIMEOUT) -R spec $(TESTS)
18+
@echo TRAVIS_JOB_ID $(TRAVIS_JOB_ID)
19+
@cat ./coverage/lcov.info | $(COVERALLS) && rm -rf ./coverage
20+
21+
test-all: test test-coveralls
22+
23+
.PHONY: test

README.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
wechat-oauth
2+
===============
3+
4+
微信公共平台OAuth接口消息接口服务中间件与API SDK
5+
6+
## 模块状态
7+
8+
- [![NPM version](https://badge.fury.io/js/wechat-oauth.png)](http://badge.fury.io/js/wechat-oauth)
9+
- [![Build Status](https://travis-ci.org/node-webot/wechat-oauth.png?branch=master)](https://travis-ci.org/node-webot/wechat-oauth)
10+
- [![Dependencies Status](https://david-dm.org/node-webot/wechat-oauth.png)](https://david-dm.org/node-webot/wechat-oauth)
11+
- [![Coverage Status](https://coveralls.io/repos/node-webot/wechat-oauth/badge.png)](https://coveralls.io/r/node-webot/wechat-oauth)
12+
13+
## 功能列表
14+
- OAuth授权
15+
- 获取基本信息
16+
17+
OAuth2.0网页授权,使用此接口须通过微信认证,如果用户在微信中(Web微信除外)访问公众号的第三方网页,公众号开发者可以通过此接口获取当前用户基本信息(包括昵称、性别、城市、国家)。详见:[官方文档](http://mp.weixin.qq.com/wiki/index.php?title=网页授权获取用户基本信息)
18+
19+
详细参见[API文档](http://node-webot.github.io/wechat-oauth/api.html)
20+
21+
## Installation
22+
23+
```sh
24+
$ npm install wechat-oauth
25+
```
26+
27+
## Usage
28+
29+
### 初始化
30+
引入OAuth并实例化
31+
32+
```js
33+
var OAuth = require('wechat-oauth');
34+
var client = new OAuth('your appid', 'your secret');
35+
```
36+
37+
以上即可满足单进程使用。
38+
当多进程时,token需要全局维护,以下为保存token的接口。
39+
40+
```js
41+
var oauthApi = new OAuth('appid', 'secret', function (openid, callback) {
42+
// 传入一个根据openid获取对应的全局token的方法
43+
fs.readFile(openid +':access_token.txt', 'utf8', function (err, txt) {
44+
if (err) {return callback(err);}
45+
callback(null, JSON.parse(txt));
46+
});
47+
}, function (openid, token, callback) {
48+
// 请将token存储到全局,跨进程、跨机器级别的全局,比如写到数据库、redis等
49+
// 这样才能在cluster模式及多机情况下使用,以下为写入到文件的示例
50+
// 持久化时请注意,每个openid都对应一个唯一的token!
51+
fs.writeFile(openid + ':access_token.txt', JSON.stringify(token), callback);
52+
});
53+
```
54+
55+
## 引导用户
56+
生成引导用户点击的URL
57+
58+
```js
59+
var url = client.getAuthorizeURL('redirectUrl', 'state', 'scope');
60+
```
61+
62+
用户点击上步生成的URL后会被重定向到上步设置的 `redirectUrl`,并且会带有`code`参数,我们可以使用这个`code`换取`access_token`和用户的`openid`
63+
64+
```js
65+
client.getAccessToken('code', function (err, result) {
66+
var accessToken = result.data.access_token;
67+
var openid = result.data.openid;
68+
});
69+
```
70+
71+
如果我们生成引导用户点击的URL中`scope`参数值为`snsapi_userinfo`,接下来我们就可以使用`openid`换取用户详细信息(必须在getAccessToken方法执行完成之后)
72+
73+
```js
74+
client.getUser('openid', function (err, result) {
75+
var userInfo = result;
76+
});
77+
```
78+
79+
## 捐赠
80+
如果您觉得Wechat OAuth对您有帮助,欢迎请作者一杯咖啡
81+
82+
![捐赠wechat](https://cloud.githubusercontent.com/assets/327019/2941591/2b9e5e58-d9a7-11e3-9e80-c25aba0a48a1.png)
83+
84+
或者[![](http://img.shields.io/gratipay/JacksonTian.svg)](https://www.gittip.com/JacksonTian/)
85+
86+
## 交流群
87+
QQ群:157964097,使用疑问,开发,贡献代码请加群。
88+
89+
## Contributors
90+
感谢以下贡献者:
91+
92+
```
93+
$ git summary
94+
95+
project : wechat
96+
repo age : 1 year, 10 months
97+
active : 122 days
98+
commits : 287
99+
files : 33
100+
authors :
101+
239 Jackson Tian 83.3%
102+
10 yelo 3.5%
103+
9 ifeiteng 3.1%
104+
4 realdog 1.4%
105+
4 Bruce Lee 1.4%
106+
3 Guo Yu 1.0%
107+
2 zhongao 0.7%
108+
2 Jesse Yang 0.7%
109+
2 Lu Jun 0.7%
110+
2 dan 0.7%
111+
1 Rogerz Zhang 0.3%
112+
1 Chen Wei 0.3%
113+
1 feichang.wyl 0.3%
114+
1 feit 0.3%
115+
1 Qun Lin 0.3%
116+
1 p13766 0.3%
117+
1 LiSheep 0.3%
118+
1 xianda 0.3%
119+
1 Lance Li 0.3%
120+
1 TooBug 0.3%
121+
122+
```
123+
124+
## License
125+
The MIT license.

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./lib/oauth');

0 commit comments

Comments
 (0)