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

微信公众平台 调用令牌 只能存活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   公众号调入令牌  操作小程序需要用的
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值