{"id":19035495,"url":"https://github.com/node-webot/co-wechat-oauth","last_synced_at":"2025-05-10T19:39:01.212Z","repository":{"id":43708439,"uuid":"41583550","full_name":"node-webot/co-wechat-oauth","owner":"node-webot","description":"Wechat OAuth for ES6.","archived":false,"fork":false,"pushed_at":"2022-02-22T14:43:50.000Z","size":17,"stargazers_count":105,"open_issues_count":3,"forks_count":29,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-05-10T07:58:51.874Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/node-webot.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-08-29T06:42:05.000Z","updated_at":"2024-05-25T13:29:39.000Z","dependencies_parsed_at":"2022-09-26T17:00:44.438Z","dependency_job_id":null,"html_url":"https://github.com/node-webot/co-wechat-oauth","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-webot%2Fco-wechat-oauth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-webot%2Fco-wechat-oauth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-webot%2Fco-wechat-oauth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/node-webot%2Fco-wechat-oauth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/node-webot","download_url":"https://codeload.github.com/node-webot/co-wechat-oauth/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253474022,"owners_count":21914223,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T21:50:59.212Z","updated_at":"2025-05-10T19:39:01.183Z","avatar_url":"https://github.com/node-webot.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"co-wechat-oauth\n===============\n\nWechat OAuth for ES6。微信公共平台OAuth接口消息接口服务中间件与API SDK\n\n## 模块状态\n\n- [![NPM version](https://badge.fury.io/js/co-wechat-oauth.png)](http://badge.fury.io/js/co-wechat-oauth)\n- [![Build Status](https://travis-ci.org/node-webot/co-wechat-oauth.png?branch=master)](https://travis-ci.org/node-webot/co-wechat-oauth)\n- [![Dependencies Status](https://david-dm.org/node-webot/co-wechat-oauth.png)](https://david-dm.org/node-webot/co-wechat-oauth)\n- [![Coverage Status](https://coveralls.io/repos/node-webot/co-wechat-oauth/badge.png)](https://coveralls.io/r/node-webot/co-wechat-oauth)\n\n## 功能列表\n\n- OAuth授权\n- 获取基本信息\n\nOAuth2.0网页授权，使用此接口须通过微信认证，如果用户在微信中（Web微信除外）访问公众号的第三方网页，公众号开发者可以通过此接口获取当前用户基本信息（包括昵称、性别、城市、国家）。详见：[官方文档](http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html)\n\n详细参见[API文档](http://doxmate.cool/node-webot/co-wechat-oauth/api.html)\n\n## Installation\n\n```sh\n$ npm install co-wechat-oauth\n```\n\n## Usage\n\n### 初始化\n\n引入 OAuth 并实例化\n\n```js\nvar OAuth = require('co-wechat-oauth');\nvar client = new OAuth('your appid', 'your secret');\n```\n\n以上即可满足单进程使用。\n当多进程时，token 需要全局维护，以下为保存 token 的接口。\n\n```js\nconst util = require('util');\nconst fs = require('fs');\n\nconst readFile = util.promisify(fs.readFile);\nconst writeFile = util.promisify(fs.writeFile);\n\nvar oauthApi = new OAuth('appid', 'secret', async function (openid) {\n  // 传入一个根据 openid 获取对应的全局 token 的方法\n  var txt = await readFile(openid +':access_token.txt', 'utf8');\n  return JSON.parse(txt);\n}, async function (openid, token) {\n  // 请将 token 存储到全局，跨进程、跨机器级别的全局，比如写到数据库、redis 等\n  // 这样才能在 cluster 模式及多机情况下使用，以下为写入到文件的示例\n  // 持久化时请注意，每个openid都对应一个唯一的token!\n  await writeFile(openid + ':access_token.txt', JSON.stringify(token));\n});\n```\n\n### 引导用户\n\n生成引导用户点击的 URL。\n\n```js\nvar url = client.getAuthorizeURL('redirectUrl', 'state', 'scope');\n```\n\n如果是PC上的网页，请使用以下方式生成\n\n```js\nvar url = client.getAuthorizeURLForWebsite('redirectUrl');\n```\n\n### 获取 Openid 和 AccessToken\n\n用户点击上步生成的 URL 后会被重定向到上步设置的 `redirectUrl`，并且会带有 `code` 参数，我们可以使用这个 `code` 换取 `access_token` 和用户的`openid`\n\n```js\nasync function () {\n  var token = await client.getAccessToken('code');\n  var accessToken = token.data.access_token;\n  var openid = token.data.openid;\n}\n```\n\n\u003e 注意，因为经常会因为浏览器的后退，导致生成的地址被反复访问，这会导致 code 被反复使用而出现错误。在这一步，最佳实践是使用完 code 之后，就将用户的信息存到 session 中。再进入这个页面时，直接使用 session 中的数据。\n\n### 获取用户信息\n\n如果我们生成引导用户点击的 URL 中 `scope` 参数值为 `snsapi_userinfo`，接下来我们就可以使用 `openid` 换取用户详细信息（必须在 getAccessToken 方法执行完成之后）\n\n```js\nasync function () {\n  var userInfo = await client.getUser('openid');\n}\n```\n\n## 捐赠\n如果您觉得Wechat OAuth对您有帮助，欢迎请作者一杯咖啡\n\n![捐赠wechat](https://cloud.githubusercontent.com/assets/327019/2941591/2b9e5e58-d9a7-11e3-9e80-c25aba0a48a1.png)\n\n或者[![](http://img.shields.io/gratipay/JacksonTian.svg)](https://www.gittip.com/JacksonTian/)\n\n## 交流群\nQQ群：157964097，使用疑问，开发，贡献代码请加群。\n\n## Contributors\n感谢以下贡献者：\n\n```\n$ git summary\n\n project  : co-wechat-oauth\n repo age : 1 year, 8 months\n active   : 8 days\n commits  : 16\n files    : 11\n authors  :\n    13  Jackson Tian  81.2%\n     2  linkkingjay   12.5%\n     1  wangxiuwen    6.2%\n\n```\n\n## License\nThe MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnode-webot%2Fco-wechat-oauth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnode-webot%2Fco-wechat-oauth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnode-webot%2Fco-wechat-oauth/lists"}