• 公众号openApi管理


    代码获取编号:001

    功能说明

    官方文档地址是:https://developers.weixin.qq.com/doc/offiaccount/openApi/clear_quota.html
    分为3部分

    • 查询 openAPI 调用quota
    • 清空 api 的调用quota
    • 查询 rid 信息

    前置条件

    进入公众号后台,在“基本配置”中,将调用接口的IP地址添加为白名单。
    需要先获取access_token,作为所有接口调用的参数。

    查询配额

    不太清楚为什么官方文档一定要把配额写成quota,个人觉得中文更好理解。
    上一篇写到每个api都有调用额度限制,比如最常用的获取access_token,一天最多获取2000次;又比如上一篇用到的网络检测接口,限制每天调用1440次。查询配额接口就是用来查询剩余配额情况的,方便再配额即将用光或者已经用光的情况下进行清空(会恢复到最大值)。

    		// 先获取access_token,这部分正式环境需要配置定时获取,每天2000次调用限制
    		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APPID + "&secret=" + SECRET;
    		String result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
    		System.out.println(result);
    		String accessToken = JSON.parseObject(result).getString("access_token");
    		//  查询网络检测配额
    		url = "https://api.weixin.qq.com/cgi-bin/openapi/quota/get?access_token=" + accessToken;
    		JSONObject param = new JSONObject();
    		param.put("cgi_path", "/cgi-bin/callback/check");
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute().body();
    		System.out.println(result);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    如果运行正常,会打印出剩余的配额。返回格式大体为:

    {"errcode":0,"errmsg":"ok","quota":{"daily_limit":1440,"used":1,"remain":1439}}
    
    • 1

    清空配额

    每个月可调用10次清零机会,避免配额耗尽引发异常。
    清空配额的含义就是当已分配的配额比如获取ip地址的每天1440次用光以后,调用一下又恢复到1440次。官方有了次数限制,这个必然不能作为长期使用的方式。
    还是用上面获取ip地址为例:

    • 调用一次网络检测接口
    • 查询网络检测接口配额(已使用不是0了)
    • 清空配额
    • 查询网络检测接口配额(恢复已使用是0,未使用1440)
    		// 先获取access_token,这部分正式环境需要配置定时获取,每天2000次调用限制
    		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APPID + "&secret=" + SECRET;
    		String result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
    		System.out.println(result);
    		String accessToken = JSON.parseObject(result).getString("access_token");
    		// 调用一次网络检测接口,用于测试查询配额剩余情况
    		url = "https://api.weixin.qq.com/cgi-bin/callback/check?access_token=" + accessToken;
    		JSONObject param = new JSONObject();
    		param.put("action", "all");
    		param.put("check_operator", "DEFAULT");
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute().body();
    		System.out.println(result);
    		// 查询网络检测配额
    		url = "https://api.weixin.qq.com/cgi-bin/openapi/quota/get?access_token=" + accessToken;
    		param = new JSONObject();
    		param.put("cgi_path", "/cgi-bin/callback/check");
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute().body();
    		System.out.println(result);
    		// 清空配额
    		url = "https://api.weixin.qq.com/cgi-bin/clear_quota?access_token=" + accessToken;
    		param = new JSONObject();
    		param.put("appid", APPID);
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute().body();
    		System.out.println(result);
    		// 必须暂停下,微信端生效需要一定时间
    		Thread.sleep(2000);
    		// 查询配额
    		url = "https://api.weixin.qq.com/cgi-bin/openapi/quota/get?access_token=" + accessToken;
    		param = new JSONObject();
    		param.put("cgi_path", "/cgi-bin/callback/check");
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute().body();
    		System.out.println(result);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    打印结果为

    {"errcode":0,"errmsg":"ok","quota":{"daily_limit":1440,"used":2,"remain":1438}}
    {"errcode":0,"errmsg":"ok"}
    {"errcode":0,"errmsg":"ok","quota":{"daily_limit":1440,"used":0,"remain":1440}}
    
    • 1
    • 2
    • 3

    可以发现最终配额又恢复到了最大值。

    查询rid信息

    对接微信公众号的时候,难免看到错误信息,同时伴随着一条含有rid的errmsg。可以根据这个rid,查询最近7天对应详细错误,方便排查。
    举个例子:清空配额的时候,需要传递appid,如果把appid传递错误,就肯定会返回错误信息,我们把错误信息信息中rid提取出来,就可以使用 查询rid信息 接口获知详细原因了。
    这个不适合收到错误rid就立刻调用,需要微信缓存一阵子才能查询到。

    		// 这部分测试获取rid信息。先调用清空配额接口,故意把appid写错
    		url = "https://api.weixin.qq.com/cgi-bin/clear_quota?access_token=" + accessToken;
    		param = new JSONObject();
    		param.put("appid", APPID+"234");
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute().body();
    		System.out.println(result);
    		String rid = JSON.parseObject(result).getString("errmsg").split(":")[1].trim();
    		// 这里必须暂停,给微信点时间缓一下
    		Thread.sleep(6000);
    		// 查询上面rid对应的详细错误信息
    		url = "https://api.weixin.qq.com/cgi-bin/openapi/rid/get?access_token=" + accessToken;
    		param = new JSONObject();
    		param.put("rid", rid);
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute().body();
    		System.out.println(result);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    打印结果:

    {"errcode":40013,"errmsg":"invalid appid rid: 6326ce0a-33d0d6d0-672a95dd"}
    {"errcode":0,"errmsg":"ok","request":{"invoke_time":1663487498,"cost_in_ms":6,"request_url":"access_token=60_Yx0by_JLDemp4tieyd71fzTmdblS9QVQXLViBCFRN-VISqEFZwuEHd0HaxcygqGU7LH8S5qaKcZHODMAbueqA6DukWaefOs-wFnpuZKcBtcEoBg6qN2GsrS8psUnpbZwMXkp_7_KcJ6rVRRfVDWiAAAFNG","request_body":"{\"appid\":\"wx276049d6a7551dca234\"}","response_body":"{\"errcode\":40013,\"errmsg\":\"invalid appid rid: 6326ce0a-33d0d6d0-672a95dd\"}","client_ip":"106.47.67.229"}}
    
    • 1
    • 2

    完整代码

    上面提到的步骤所有代码如下

    package com.lootaa.wechat;
    
    import org.jsoup.Jsoup;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    
    import org.jsoup.Connection.Method;
    
    /**
     * 前置条件:公众号后台设置ip白名单
     */
    public class Test001 {
    
    	public static final String APPID = "wx276049d6a7551dca";
    	public static final String SECRET = "cbe109fdf6f399bd72ed3a4afafa21b1";
    	
    	/**
    	 * 完整项目源码可关注公众号"lootaayun"(洛塔),回复001获取
    	 */
    	public static void main(String[] args) throws Exception {
    		// 先获取access_token,这部分正式环境需要配置定时获取,每天2000次调用限制
    		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APPID + "&secret=" + SECRET;
    		String result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
    		System.out.println(result);
    		String accessToken = JSON.parseObject(result).getString("access_token");
    		// 调用一次网络检测接口,用于测试查询配额剩余情况
    		url = "https://api.weixin.qq.com/cgi-bin/callback/check?access_token=" + accessToken;
    		JSONObject param = new JSONObject();
    		param.put("action", "all");
    		param.put("check_operator", "DEFAULT");
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute().body();
    		System.out.println(result);
    		// 查询网络检测配额
    		url = "https://api.weixin.qq.com/cgi-bin/openapi/quota/get?access_token=" + accessToken;
    		param = new JSONObject();
    		param.put("cgi_path", "/cgi-bin/callback/check");
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute().body();
    		System.out.println(result);
    		// 清空配额
    		url = "https://api.weixin.qq.com/cgi-bin/clear_quota?access_token=" + accessToken;
    		param = new JSONObject();
    		param.put("appid", APPID);
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute().body();
    		System.out.println(result);
    		// 必须暂停下,微信端生效需要一定时间
    		Thread.sleep(2000);
    		// 查询配额
    		url = "https://api.weixin.qq.com/cgi-bin/openapi/quota/get?access_token=" + accessToken;
    		param = new JSONObject();
    		param.put("cgi_path", "/cgi-bin/callback/check");
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute().body();
    		System.out.println(result);
    		
    		
    		// 这部分测试获取rid信息。先调用清空配额接口,故意把appid写错
    		url = "https://api.weixin.qq.com/cgi-bin/clear_quota?access_token=" + accessToken;
    		param = new JSONObject();
    		param.put("appid", APPID+"234");
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute().body();
    		System.out.println(result);
    		String rid = JSON.parseObject(result).getString("errmsg").split(":")[1].trim();
    		// 这里必须暂停,给微信点时间缓一下
    		Thread.sleep(6000);
    		// 查询上面rid对应的详细错误信息
    		url = "https://api.weixin.qq.com/cgi-bin/openapi/rid/get?access_token=" + accessToken;
    		param = new JSONObject();
    		param.put("rid", rid);
    		result = Jsoup.connect(url).ignoreContentType(true).method(Method.POST).requestBody(param.toString()).execute().body();
    		System.out.println(result);
    		
    	}
    	
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
  • 相关阅读:
    前端ES6 面试过关宝典
    ubuntu 20.04 LTS 安装 GitLab
    pytorch基础学习(4)
    JAVA毕业设计家教到家平台计算机源码+lw文档+系统+调试部署+数据库
    Linux 串口应用编程
    Mysql:设置主键自动增长起始值
    做UI设计师是否需要美术功底?
    活动回顾 | 基于英特尔技术的端到端音视频优化
    初识Golang的面向对象 为结构体(类)绑定方法
    Thinking for Doing:让LLMs能推断他人心理状态来做出适当的行动。
  • 原文地址:https://blog.csdn.net/m0_58095675/article/details/126918725