收藏
回答

怎么解决调用订阅API时OpenId无效的问题?

我的openId是从后端服务器获取下来的,调用其他API用这个openid都没有问题,但是当我调用“发送订阅消息”API的时候就会报“40003,invalid openid hint: [iP0pua02568648]”,我用“微信公众平台接口调试工具”调试还是会报相同的错误,请各位指教!

  1. access_token确认没有问题

  2. OpenId确认没有问题

  3. appKey确认没有问题

  4. 小程序目前处于开发阶段,没有上线



这我发送订阅消息的服务端代码

package com.yuezhi.controller.wxsmallapppush;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.yuezhi.entity.wxsmallapppush.Token;
import com.yuezhi.entity.wxsmallapppush.WXSmallTemplate;
import com.yuezhi.entity.wxsmallapppush.WXSmallTemplateParam;
import com.yuezhi.util.JsonView;
import com.yuezhi.util.wechat.WeChatLoginToGetUnionId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.resource.HttpResource;
 
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * @ProjectName yuezhi
 * @ClassName WXSmallAppPush
 * @Description TODO
 * @Author Wednesday
 * @Date 2019/11/9 11:15
 * @Version 1.0
 **/
@RestController
@RequestMapping("/wxs")
public class WXSmallAppPush {
 
    @RequestMapping("/wxsSend")
    public JsonView wxsSend(String openId){
        JsonView jsonView = new JsonView();
        //封装了推送实体类
        WXSmallTemplate tem = new WXSmallTemplate();
        //模板id
        tem.setTemplateId("*****");
        //推送给谁
        tem.setToUser(openId);
        //用户点击之后调到小程序哪个页面
        tem.setPage("pages/login/login");
        //又封装了一个实体类
        List<WXSmallTemplateParam> paras = new ArrayList<WXSmallTemplateParam>();
        //添加对应参数
        paras.add(new WXSmallTemplateParam("thing1", "小程序测试"));
        paras.add(new WXSmallTemplateParam("time2", "2019-11-09 11:33"));
 
        //然后把所有参数扔到大的模板中
        tem.setTemplateParamList(paras);
        //获取token凭证。
        Token token = getToken();
        String accessToken = token.getAccessToken();
        //最后一步请求接口哈哈哈  推送成功
        boolean result1 = sendTemplateMsg(accessToken, tem);
        if (result1) {
            System.err.println("推送成功");
        } else {
            System.err.println("推送失败");
        }
        return jsonView;
    }
 
 
    private static final String APPID = "****";// 微信应用唯一标识
    private static final String SECRET = "*****";
    private static String token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
 
 
    private static Logger logger = LoggerFactory.getLogger(WXSmallAppPush.class);
 
    public static Token getToken() {
        Token token = null;
        String requestUrl = String.format(token_url, APPID, SECRET);
        // 发起GET请求获取凭证
        String response = WeChatLoginToGetUnionId.httpsRequestToString(requestUrl, "GET", null);
        JSONObject jsonObject = JSON.parseObject(response);
        if (null != jsonObject) {
            try {
                token = new Token();
                token.setAccessToken(jsonObject.getString("access_token"));
                token.setExpiresIn(jsonObject.getInteger("expires_in"));
            } catch (JSONException e) {
                token = null;
                // 获取token失败
                logger.error("获取token失败 errcode:{} errmsg:{}", jsonObject.getInteger("errcode"),
                        jsonObject.getString("errmsg"));
            }
        }
        return token;
    }
 
 
    public static boolean sendTemplateMsg(String token, WXSmallTemplate template) {
 
        boolean flag = false;
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN";
        requestUrl = requestUrl.replace("ACCESS_TOKEN", token);
        System.err.println(requestUrl);
        String response = WeChatLoginToGetUnionId.httpsRequestToString(requestUrl, "POST", template.toJSON());
        JSONObject jsonResult = JSON.parseObject(response);
        System.err.println(template.toJSON());
        if (jsonResult != null) {
            Integer errorCode = jsonResult.getInteger("errcode");
            String errorMessage = jsonResult.getString("errmsg");
            if (errorCode == 0) {
                flag = true;
            } else {
                System.out.println("模板消息发送失败:" + errorCode + "," + errorMessage);
                flag = false;
            }
        }
        return flag;
    }
 
}


最后一次编辑于  2019-11-09
回答关注问题邀请回答
收藏

2 个回答

  • 盖伦会编程
    盖伦会编程
    2019-11-21

    楼主,你这边的问题解决了吗

    2019-11-21
    有用
    回复
  • 王浩Hanks🇨🇳
    王浩Hanks🇨🇳
    2019-11-09

    我看你代码发送的是get请求, 用POST吧,我刚刚看了下文档说用post哟

    2019-11-09
    有用
    回复 1
    • 盖伦会编程
      盖伦会编程
      2019-11-21
      他用的是get拿access_token
      2019-11-21
      回复
登录 后发表内容
问题标签