微信公众号入门

下文基本包含了80%以上源代码,如果仍然需要项目源码,可以留言信息,留言码:000。

入口

微信官方文档入口网址是https://developers.weixin.qq.com/doc/
包括小程序、小游戏、小商店、公众号、智能对话、开放平台、企业微信、微信支付、视频号、腾讯小微。
其中,公众号文档详细地址为:https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html

说明

微信的官方文档变更非常频繁,而且是很不人性化的变更。比如即将取消介入的模板消息、小程序获取手机号的方式、公众号获取粉丝信息只剩下openid和unionid、小程序即便获取位置都需要单独提交审核等等。
所以,本篇文档仅能保证在当前时间段是可以使用的,并不确保微信变更后还能继续使用。
接口调试:https://mp.weixin.qq.com/debug/

权限说明

公众号分为订阅号和服务号,其中订阅号每天可以发送1条,服务号每月发送4条。权限也各不相同,可以查看https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Explanation_of_interface_privileges.html
为了更详细说明各个功能,后续所有的测试账号均使用的已认证服务号。
也可以使用测试号:https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

错误码

错误码便于判断出现异常的原因,但是,文档最开始列出的错误码并非包含所有情况下返回的错误,很多时候还是需要进入调用接口部分查看对应错误信息。
错误码情况文档https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Global_Return_Code.html

调用频次

所有接口都有调用频次限制,比如最常用的获取access_token,一天最多获取2000次,如果每次调用都获取一次access_token,则业务较多情况下必然会失败,所以需要进行本地保存,然后定时刷新。后续所有代码,不会定时刷新,而是直接获取然后使用的方式,仅为功能展示,需要手动去调整。
具体频次限制参考https://developers.weixin.qq.com/doc/offiaccount/Message_Management/API_Call_Limits.html

公众号准备

有了已认证的服务号后,为了更好的开发,先获取到appid、appsecret,并添加ip白名单(开发电脑的ip、部署项目服务器的ip),只有白名单中的ip才能获取access_token。

入门代码

所有测试代码均适用java开发,连接使用jsoup。json格式处理使用fastjson。

		<dependency>
			<groupId>org.jsoup</groupId>
			<artifactId>jsoup</artifactId>
			<version>1.11.3</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.60</version>
		</dependency>

获取微信IP地址

如果只是练习或者规模不大的搞一搞,其实没有必要获取微信ip地址,以此来限制访问。仅做练习使用。如果用到正式环境,微信官方建议是每天调用一次更新ip。
该部分代码也用来演示如何调用微信get方式的接口。

package com.lootaa.wechat;

import org.jsoup.Jsoup;

import com.alibaba.fastjson.JSON;

import org.jsoup.Connection.Method;

public class Test000 {

	public static final String APPID = "wx276049d6a7551dca";
	public static final String SECRET = "cbe109fdf6f399bd72ed3a4afafa21b1";
	
	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");
		// 获取微信 API 接口 IP地址
		url = "https://api.weixin.qq.com/cgi-bin/get_api_domain_ip?access_token=" + accessToken;
		result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
		System.out.println(result);
		// 获取微信callback IP地址
		url = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=" + accessToken;
		result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
		System.out.println(result);
	}
	
}

微信接口调用方式基本都一致,比如get方式调用的,都是上面代码中的形式,修改下url即可。
获取access_token的响应结果

{  
"access_token":"60_l6wJ-f8C36YZihN1Yh3Jyw-EpGN7YDQa1_s_P8kUHW828L0xlndv0p7dYGqspQZuY81J9RvcJsH9w8LTt5qomHPofiHSym0ko9TrGQsvruJ6qQNwge39Meh7UgofwFpg7RTj7nUvv3mDafaNXIDbAJADOJ",  
"expires_in":7200  
}

获取微信 API 接口 IP地址响应结果

{  
"ip_list":[  
        "101.89.47.18",  
        "101.91.34.103",  
        "101.91.37.13",  
        "109.244.129.223",  
        "109.244.145.152",  
        "109.244.184.250",  
        "112.53.42.235",  
        "112.60.20.154",  
        "112.65.193.153",  
        "112.90.80.215",  
        "116.128.170.42",  
        "116.128.184.169",  
        "117.144.228.18",  
        "117.144.228.62",  
        "119.147.6.203",  
        "119.147.6.237",  
        "120.232.65.161",  
        "157.148.36.94",  
        "157.255.218.109",  
        "175.27.18.18",  
        "175.27.5.221",  
        "183.2.143.222",  
        "203.205.239.82",  
        "203.205.239.94",  
        "221.181.99.40",  
        "81.69.216.43"  
    ]  
}

获取微信callback IP地址响应结果

{  
"ip_list":[  
        "106.55.206.146",  
        "106.55.206.211",  
        "106.55.207.148",  
        "106.55.207.31",  
        "118.126.124.186",  
        "118.126.124.246",  
        "119.29.180.49",  
        "119.29.9.101",  
        "124.223.151.119",  
        "129.226.105.242",  
        "162.62.80.57",  
        "162.62.80.8",  
        "162.62.81.123",  
        "175.24.211.142",  
        "175.24.211.157",  
        "175.24.211.198",  
        "175.24.211.31",  
        "175.24.211.4",  
        "175.24.212.10",  
        "175.24.212.185",  
        "175.24.212.195",  
        "175.24.213.149",  
        "175.24.214.150",  
        "175.24.214.222",  
        "175.27.64.215",  
        "175.27.65.216",  
        "42.192.0.152",  
        "42.192.0.224",  
        "42.192.6.57",  
        "43.132.112.30",  
        "43.132.141.238",  
        "43.137.147.169",  
        "43.143.52.12",  
        "43.143.59.119",  
        "81.69.101.193",  
        "81.69.101.225",  
        "81.69.101.233",  
        "81.69.101.237",  
        "81.69.103.129",  
        "81.69.103.147",  
        "81.69.103.163",  
        "81.69.103.205",  
        "81.69.103.236",  
        "81.69.103.238",  
        "81.69.103.37",  
        "81.69.103.74",  
        "81.71.140.59",  
        "81.71.19.101"  
    ]  
}

网络监测

该部分用来介绍如何调用微信post方式带参数的接口。
和上面样例的代码放一起了。

package com.lootaa.wechat;

import org.jsoup.Jsoup;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.jsoup.Connection.Method;

public class Test000 {

	public static final String APPID = "wx276049d6a7551dca";
	public static final String SECRET = "cbe109fdf6f399bd72ed3a4afafa21b1";
	
	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");
		// 获取微信 API 接口 IP地址
		url = "https://api.weixin.qq.com/cgi-bin/get_api_domain_ip?access_token=" + accessToken;
		result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
		System.out.println(result);
		// 获取微信callback IP地址
		url = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=" + accessToken;
		result = Jsoup.connect(url).ignoreContentType(true).method(Method.GET).execute().body();
		System.out.println(result);
		// 网络检测
		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);
	}
	
}

响应结果为

{  
"dns":[  
        {  
            "ip":"82.156.8.222",  
            "real_operator":"CAP"  
        }  
    ],  
"ping":[  
        {  
            "ip":"82.156.8.222",  
            "from_operator":"CAP",  
            "package_loss":"0%",  
            "time":"26.227ms"  
        }  
    ]  
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

lootaa

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

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

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

打赏作者

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

抵扣说明:

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

余额充值