快捷搜索: 王者荣耀 脱发

微信公众平台-测试号网页授权-获取openid方法

1、创建自己的测试号

2、测试号管理信息填写(注意仔细一步步对照)

2.接口配置信息

    Controller测试代码:

3.JS接口安全域名

此处还有点问题,后面具体使用时再来完善,因为一个月只有三次修改机会,不敢随便修改

3、手动获取openid

文档说明中使用步骤: 1.获取code

package com.mys.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author mys
 * @date 2022/3/23 15:02
 */
@RestController
@RequestMapping("/weixin")
@Slf4j
public class WeixinController {
          
   

    @GetMapping("/auth")
    public void auth(@RequestParam("code") String code) {
          
   
        log.info("进入auth方法");
        log.info("code={}", code);
    }
}

运行结果如下: 2.获取access_token

第1步获取到code之后,用下面的请求链接,获取access_token,会返回一个json格式的数据,其中里面就包含了openid,这个很重要的信息,用于唯一标识用户

链接需要修改的地方:填写自己的appid、secret,上面测试号管理信息中有,code换成第1步获取到的code,具体可查看测试代码 测试代码:

运行结果: 复制出来的json数据如下:

4、使用SDK获取openid

1.引入依赖 pom.xml

<dependency>
	<groupId>com.github.binarywang</groupId>
    <artifactId>weixin-java-mp</artifactId>
    <version>4.2.0</version>
</dependency>

2.修改配置文件 application.yml,并写配置 WechatAccountConfig

package com.mys.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author mys
 * @date 2022/3/23 15:53
 */
@Data
@Component
@ConfigurationProperties(prefix = "wechat")
public class WechatAccountConfig {
          
   

    /**
     * 公众平台id
     */
    private String mpAppId;

    /**
     * 公众平台密钥
     */
    private String mpAppSecret;
}

WechatMpConfig

package com.mys.config;

import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @author mys
 * @date 2022/3/23 15:47
 */
@Component
public class WechatMpConfig {
          
   

    @Autowired
    private WechatAccountConfig accountConfig;

    @Bean
    public WxMpService wxMpService() {
          
   
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }

    @Bean
    public WxMpConfigStorage wxMpConfigStorage() {
          
   
        WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
        wxMpConfigStorage.setAppId(accountConfig.getMpAppId());
        wxMpConfigStorage.setSecret(accountConfig.getMpAppSecret());
        return wxMpConfigStorage;
    }
}

3.查看文档,编写代码 文档链接:https://github.com/Wechat-Group/WxJava/wiki/%E5%85%AC%E4%BC%97%E5%8F%B7%E5%BC%80%E5%8F%91%E6%96%87%E6%A1%A3 也就是上面参考文档里面的一个子链接,下面代码就是一个获取openid的案例,其他学习可查看文档,写的很全面。

调试运行结果: 很想感叹一下,看中文的注释太清晰了!

经验分享 程序员 微信小程序 职场和发展