微信公众号测试号接口配置报错 {errcode“:-106,“errmsg“:“token check fail“}

微信公众号测试号接口配置报错

结论

如果你的公网接口是正常的,直接返回echostr也会校验失败!那么肯定是返回值不对!!!
返回值必须为整数,不能为字符串,微信服务器可接收的数据类型是:content-type: text/html; charset=utf-8

@RequestMapping(value = "/validate", produces = "text/plain;charset=UTF-8")
    public Long validate(String signature, String timestamp, String nonce, String echostr, HttpServletResponse response) {
        return wechatService.validate(signature, timestamp, nonce, echostr);
    }

代码

@RequestMapping("/wechat")
@RestController
public class WechatController {

    @Resource
    private WechatService wechatService;

    /**
     * 接口配置信息验证,返回值一定要是Long,不能为String
     *
     * @param signature 签名
     * @param timestamp 时间戳
     * @param nonce     nonce
     * @param echostr   echostr
     * @param response  回答
     * @return {@link Long }
     * @author huxp
     */
    @RequestMapping(value = "/validate", produces = "text/plain;charset=UTF-8")
    public Long validate(String signature, String timestamp, String nonce, String echostr, HttpServletResponse response) {
        return wechatService.validate(signature, timestamp, nonce, echostr);
    }
}
@Slf4j
@Service
public class WechatServiceImpl implements WechatService {

    public static final String weChatToken = "xxxxxx";
    public static final String weChatAppId = "xxxxxxxxx";
    public static final String weChatAppSecret = "xxxxxxxxxxx";
    
    /**
     * 验证消息来自微信服务器
     *
     * @param signature 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
     * @param timestamp 时间戳
     * @param nonce     随机数
     * @param echostr   随机字符串
     * @return 随机字符串
     */
    @Override
    public Long validate(String signature, String timestamp, String nonce, String echostr) {

        //获取参数配置
        log.info("signature: {}, timestamp: {}, nonce: {}, echostr: {}", signature, timestamp, nonce, echostr);
        String[] arrTmp = {weChatToken, timestamp, nonce};
        Arrays.sort(arrTmp);
        StringBuilder sb = new StringBuilder();
        for (String s : arrTmp) {
            sb.append(s);
        }
        try {
            String sha1Hex = DigestUtils.sha1Hex(sb.toString());
            log.info("sha1Hex: {}", sha1Hex);
            log.info("signature: {}", signature);
            //获取消息对象
            if (signature.contentEquals(sha1Hex)) {
                return Long.parseLong(echostr.trim());
            }
        } catch (Exception e) {
            log.error("校验失败: ", e);
        }
        return null;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码神附体

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值