微信公众号对接

1.认证服务器,使公众号开发后可以调用该服务器的下的接口(js同)
在这里插入图片描述

@RestController
public class WeixinController {
    private Logger logger = LoggerFactory.getLogger(WeixinController.class);

    @GetMapping("")//(value = "/",produces = { "application/json;charset=utf-8" })//
    public String getWxRequest(@RequestParam(required = false) String echostr,
                               @RequestParam(required = false) String signature,
                               @RequestParam(required = false) String timestamp,
                               @RequestParam(required =false) String nonce){
        String token="weixin";
        //1)将token、timestamp、nonce三个参数进行字典序排序
        List<String> list=new ArrayList();
        list.add(token);
        list.add(timestamp);
        list.add(nonce);
        Collections.sort(list);
        //2)将三个参数字符串拼接成一个字符串进行sha1加密
        String sha1Hex = DigestUtils.sha1Hex(list.get(0) + list.get(1) + list.get(2));
        logger.info("sha1Hex:{}",sha1Hex);
        //将加密与signature比对,结果一致说明来自微信服务器,返回echostr
        if (sha1Hex.equals(signature)){
            return echostr;
        }
        return null;
    }
}

js直接配置就可以
在这里插入图片描述

设置授权的网站

在这里插入图片描述

先开发自定义菜单模块,设置网页入口,然后进入网页开发(授权只是为了获取用户信息等)
在这里插入图片描述

2.获取用户access_token

String url=" https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
        url= String.format(url,"wxd51c370ef340ace1","a47f52d802dca90063816c64b2f59aca");
        //application/x-www-form-urlencoded
        Connection.Response response = Jsoup.connect(url)
                .ignoreContentType(true)
                .header("Accept", "application/json, text/plain, */*")
                // .header("Content-Type", "application/json;charset=UTF-8")
                .method(Connection.Method.GET).execute();
        String body = response.body();
        JSONObject jsonObject = JSONObject.parseObject(body);
        System.out.println(jsonObject);

=======微信网页开发

3.通过oauth2授权获取登录令牌access_token(不同于用户access_token)

/*通过这个页面授权*/
    @GetMapping("/oauth")
    public void oauth(HttpServletResponse response) throws IOException {
        //配置域名不带http或者https===》shaofeng.free.idcfengye.com (~~~挂载域名后面的都可以授权)
        //获取code
        String url="https://open.weixin.qq.com/connect/oauth2/authorize?" +
                "appid=%s" +
                "&redirect_uri=%s" +
                "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
        url= String.format(url,"wxd51c370ef340ace1","http://shaofeng.free.idcfengye.com/getUserInfo");
        //url是上面配置的授权url域名下的
        //用户确认授权跳转到redirect_uri
        response.sendRedirect(url);

    }

    @GetMapping("/getUserInfo")
    public String getUserInfo(HttpServletRequest request) throws IOException{
        String code = request.getParameter("code");
        String url1="https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
        url1= String.format(url1,"wxd51c370ef340ace1","a47f52d802dca90063816c64b2f59aca",code);

        Connection.Response response1 = Jsoup.connect(url1)
                .ignoreContentType(true)
                .header("Accept", "application/json, text/plain, */*")
                // .header("Content-Type", "application/json;charset=UTF-8")
                .method(Connection.Method.POST).execute();

        System.out.println(JSONObject.parseObject(response1.body()));
        return JSONObject.parseObject(response1.body()).getString("access_token");

    }
### 使用Python实现微信公众号口开发 #### 准备工作 为了使用 Python 进行微信公众号的开发,开发者需先注册并认证一个微信公众平台账号。完成之后,在公众平台上获取 AppID 和 AppSecret 是必要的,因为这些信息用于后续 API 调用的身份验证。 #### 安装依赖库 推荐安装 `werobot` 库简化与微信服务器之间的交互过程。可以通过 pip 工具轻松安装此库: ```bash pip install werobot ``` #### 创建基本应用框架 下面是一个简单的例子展示如何创建一个能够响应消息的基础机器程序[^3]: ```python import werobot # 初始化 WeRoBot 实例, 替换 'tokenhere' 为实际 token 值 robot = werobot.WeRoBot(token='tokenhere') @robot.handler def hello(message): """每当收到任何类型的消息时触发""" return 'Hello World!' if __name__ == '__main__': # 配置 Webhook 的地址和端口 robot.config['HOST'] = '0.0.0.0' robot.config['PORT'] = 80 # 启动内置 web server 并保持运行状态等待请求到来 robot.run() ``` 这段代码实现了当用户发送任意内容给该公众号时,它会自动回复 "Hello World!" 给对方。 #### 处理不同类型的消息 除了简单地返回固定字符串作为回应之外,还可以根据不同种类的消息做出更复杂的反应。比如处理文字、图片或者其他多媒体形式的信息,并据此执行特定逻辑或调用微信提供的其他功能APIs 来增强互动体验。 对于更加复杂的应用场景,则可能涉及到 OAuth 授权流程以及更多高级特性如模板消息推送等功能集成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值