如何获取微信用户openid

1、前言

2、手工方式

2.1、设置域名

哈哈,这个网站上面的域名也不是特别的贵呀,我在这上面买的一个域名为期一个月的话也就才12元,且改类型的属于二级域名,是已经备过案的,所以也就不需要备案。

(3). 下载对应的客户端进行启动


  1. 在windows上启动的命令
natapp -authtoken 你的authtoken
  1. 启动后

可见我的域名指向了127.0.0.1:8080。

提交之前我们需要将上图中的红色框框住的部分的文件下载下来放置项目的static目录下,测试访问通过之后,然后才能进行提交。

2.2、获取code

    (1).登录自己的测试号

    (2).编写对应的接口
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;

/**
 * @author :小肖
 * @date :Created in 2022/2/1 21:55
 */
@RestController
@RequestMapping("/weixin")
@Slf4j
public class WeixinController {
          
   

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

    (3).在登录测试号之后进行网页授权

授权的域名就是我们在natapp.cn上购买的域名,如果没有进行授权的话那么就会报出 10003 redirect_uri域名与后台配置不一致 错误。
    (4).进行访问url进行测试

注意点

    (5).测试结果

成功获取了用户的code信息。

2.3、换取access_token

    (1).编写的controller
    (2).访问的url组成
参数 是否必须 说明 code 是 填写第一步获取的code参数 grant_type 是 填写为authorization_code
    (3).访问的结果
{
          
   
  "access_token": "53_HK355v2MhOolNlGkaoUf4oDCkyX0WDollvsQNU5SvhsvmvF2S2VoqdPXuokfERI2oqFvQijVShq8aQzeQ9n01mGKSJn7q5rLAcYbTjm1H7k",
  "expires_in": 7200,
  "refresh_token": "53_C1us_G770mgzXjd-PuK329qB65lXiK483_qxUXjKudwWIdHkOz5ntwlByEgUQfMEy_-7tCCzcO4DoHaFbY0JurpZYD3Bys6DLs8ua8J_CjU",
  "openid": "你的openid",
  "scope": "snsapi_base"
}

3、使用第三方sdk

3.1、引入第三方依赖

3.3、编写配置类初始化设置wxMpService配置

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 :小肖
 * @date :Created in 2022/2/2 10:24
 */
@Component
public class WechatMpConfig {
          
   


    @Autowired
    private WechatAccountConfig wechatAccountConfig;

    @Autowired
    private WxMpInMemoryConfigStorage wxMpInMemoryConfigStorage;

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

    @Bean
    public WxMpInMemoryConfigStorage wxMpConfigStorage(){
          
   
        /**
         * 这里需要注意的是 由于父类中没有定义对应的接口
         * 所以所有的方法都在其实现类中,所以我们要构造实现类
         */
        WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
        wxMpConfigStorage.setAppId(wechatAccountConfig.getMpAppId());
        wxMpConfigStorage.setSecret(wechatAccountConfig.getMpAppSecret());
        return wxMpConfigStorage;
    }
}

3.4、编写对应的controller

3.5、进行debug测试

    第一个断点

该重定向的url很明显就是我们手工方式中获取code的url。

    第二个断点

成功获取了code和openid。

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