微信获取用户信息时部分用户 api unauthorized, hints 如何破
发布于 7 年前 作者 thomas0836 4914 次浏览 来自 问答

最近我们公司有一个产品是基于微信服务号的。其中一个功能是用户开始使用的时候就获取他们的用户信息。 功能已开发完毕,然后全公司的人都测试过可以使用。但是到了投入真实应用后,收到部分用户反馈,界面提出错误为 api unauthorized, hints: [ req_id: xxxxxxx]

不是全部用户,是小量的部分用户出现该问题,出现问题的用户平台版本各不一样,使用的手机型号也是各不一样。网上找了一遍还没有找到破解方法。

求大神指导 使用的依赖包是: “co-wechat”: “0.0.4”, “co-wechat-api”: “^2.4.0”, “co-wechat-body”: “^0.1.2”, “co-wechat-oauth”: “^1.0.1”, “co-wechat-payment”: “^0.1.6”,

代码大致

var url = wechatOAuth.getAuthorizeURL(Config.host + '/index','','snsapi_userinfo')

然后在菜单哪里把这个链接放上去,用户点击菜单后跳到index路由,路由内通过code获取到openid openid 的获取我是通过一个中间件,监测如果有 code 参数的时候就去获取

const wechatOAuth = require('../common/wechat-oauth')
const cache = require('../common/cache')
const logger = require('../common/logger')()

module.exports = function *(next) {

    const code = this.request.query.code

    if (code){
        var session = this.session
        if (!session.openid) {
            var openid = yield cache.get(`${code}:openid:${Config.session_secret}`)

            if (!openid) {
                try {
                    const accessToken = yield wechatOAuth.getAccessToken(code)
                    const token = accessToken.data.access_token
                    openid = accessToken.data.openid

                    yield cache.set(`${code}:openid:${Config.session_secret}`, JSON.stringify(openid), 3600 * 24)
                } catch (err) {
                    logger.error(err)
                }
            }
            session.openid = openid
        }
    }

    yield next
}

然后就是用这个openid 获取用户信息

var userInfo,error_msg
try {
        userInfo = yield wechatOAuth.getUser(openid)
} catch (catchErr) {
       error_msg = catchErr.message
}

就是在这里会某些用户抛出这个错误,而且是如果这个用户抛出一次就永远也是这里抛出错误,如果是没有抛出错误就永远都可以没有问题。

其他关于token 的处理如下:

"use strict"
var OAuth = require('co-wechat-oauth')
const cache = require('../common/cache')
const wechat_opts = require('../../config/wechat')


var client = new OAuth(wechat_opts.appId, wechat_opts.appSecret, function* (openid) {
    // 传入一个根据openid获取对应的全局token的方法

    var txt = yield cache.get(`${wechat_opts.appId}_${openid}_access_token_${Config.session_secret}`)
    return JSON.parse(txt);
}, function* (openid, token) {
    // 请将token存储到全局,跨进程、跨机器级别的全局,比如写到数据库、redis等
    // 这样才能在cluster模式及多机情况下使用,以下为写入到文件的示例
    // 持久化时请注意,每个openid都对应一个唯一的token!
    yield cache.set(`${wechat_opts.appId}_${openid}_access_token_${Config.session_secret}`, JSON.stringify(token), 3600 * 24)
});

module.exports = client
var WechatAPI = require('co-wechat-api')

const cache = require('../common/cache')
const wechat_opts = require('../../config/wechat')
const key = `${wechat_opts.appId}_wechat_api_access_token_${Config.session_secret}`

var api = new WechatAPI(wechat_opts.appId, wechat_opts.appSecret, function* () {
    // 传入一个获取全局token的方法
    var txt = yield cache.get(key)
    return JSON.parse(txt)
}, function* (token) {
    // 请将token存储到全局,跨进程、跨机器级别的全局,比如写到数据库、redis等
    // 这样才能在cluster模式及多机情况下使用,以下为写入到文件的示例
    yield cache.set(key, JSON.stringify(token), 3600 * 24)
})

module.exports = api

以上的代码已经获取过几千个用户信息,只是随机的有几个用户的openid 会报这样的错误,而且隔一天再去也是报一样的错误

1 回复

没有人遇到过吗?

回到顶部