微信开放平台--获取微信公众号或者小程序的调入令牌

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 17:04   1867   0

微信公众平台 调用令牌 只能存活2小时(实际更短 所以我1小时刷新一次令牌)

小程序刷新调入令牌

 /**
     * 拿去小程序的临时登录令牌
     * @param smallproinfo
     * @return
     */
    public static  Map<String,String> appToken(Smallproinfo smallproinfo, String token){
        JSONObject resultjson=new JSONObject();
        resultjson.put("component_appid", Configure.getValue("AppID"));
        resultjson.put("authorizer_appid",smallproinfo.getAppid());
        resultjson.put("authorizer_refresh_token",smallproinfo.getRefreshToken());
        JSONObject auactoKen= WxstoreUtils.httpRequest(Configure.getValue("authorizerToken")+token,"POST",resultjson.toString());
        Map<String,String> dataMap=new HashMap<String,String>();
        if(auactoKen.containsKey("errcode")&&!"0".equals(auactoKen.getString("errcode"))){
            dataMap.put("msg","小程序已经失效需要重新绑定!");
            System.out.println("---------------失败原因:"+auactoKen.get("errcode"));
            return dataMap;
        }
        dataMap.put("refreshToken",auactoKen.getString("authorizer_refresh_token"));
        dataMap.put("accesToken",auactoKen.getString("authorizer_access_token"));
        return  dataMap;
    }
component_appid   为微信开放平台的appid 
authorizer_appid  小程序的appid
authorizer_refresh_token  小程序的刷新令牌 如果没有  那你就中奖了 需要重新授权获取   这个很重要
authorizerToken  请求路径   token  是微信开放平台的token  记住是微信开放平台

返回的数据汇总

authorizer_refresh_token    小程序的刷新令牌  没有有这个小程序基本就GG   所以需要妥善保管
authorizer_access_token   小程序调入令牌  操作小程序需要用的

下面是公众号的

 /**
  * 4、获取(刷新)授权公众号的令牌
  * @param apiAuthorizerToken
  * @param component_access_token
  */
 public static ApiAuthorizerTokenRet apiAuthorizerToken(ApiAuthorizerToken apiAuthorizerToken, String component_access_token
             , String secret) throws WexinReqException{
  ApiAuthorizerTokenRet apiAuthorizerTokenRet =new ApiAuthorizerTokenRet();
  JSONObject param = JSONObject.fromObject(apiAuthorizerToken);
  String authorizerToken="https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=";
    JSONObject auactoKen=WxstoreUtils.httpRequest(authorizerToken+component_access_token,"POST",param.toString());
  if (auactoKen.has("errcode") && auactoKen==null) {//在这里用appsecret在掉一次系统
   authorizerToken=Configure.getValue("temporaryToken").replace("#appid",apiAuthorizerToken.getAuthorizer_appid())
     .replace("#secret",secret);
   auactoKen=WxstoreUtils.httpRequest(authorizerToken,"GET",null);
   if(auactoKen.has("errcode")){
    logger.error("获取微信公众号平台access_token!errcode=" + auactoKen.getString("errcode") + ",errmsg = " + auactoKen.getString("errmsg"));
    throw new WexinReqException("获取微信公众号平台access_token!errcode=" + auactoKen.getString("errcode") + ",errmsg = " + auactoKen.getString("errmsg"));
   }else{
    apiAuthorizerTokenRet.setAuthorizer_access_token(auactoKen.getString("access_token"));
   }
  }else{
   apiAuthorizerTokenRet = (ApiAuthorizerTokenRet) JSONObject.toBean(auactoKen, ApiAuthorizerTokenRet.class);
  }

  return apiAuthorizerTokenRet;
 }
ApiAuthorizerToken  3个参数    
component_appid  微信开放平台的appid     
authorizer_appid 公众号的APPid  authorizer—refresh_token  公众号的刷新令牌  这个很重要
component_access_token  微信开放平台的调入令牌
ApiAuthorizerTokenRet  里面的参数
authorizer_refresh_token    公众号刷新令牌 很重要
authorizer_access_token   公众号调入令牌  操作小程序需要用的
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP