有了微信授权才能获取openid,有了openid才能进行后面的操作,对于某个小程序或者公众号,用户的openid是唯一的

获取openid的两种方式

  • 手工方式
  • 利用第三方SDK

注册使用内网穿透工具natapp

内网穿透工具就是一个映射,他能将二级域名与本地ip端口耗对应起来,即你访问任何一个都ok,用于微信开发调试很方便

https://natapp.cn/ 购买隧道,推荐VIP_1型,然后注册二级域名,注意要购买支持微信开发的域名,填写到接口测试号的网页授权获取用户基本信息中,配置隧道的二级域名和端口号(8080)。到现在你大概花了十几块钱吧,不过没关秀,知识是无价的,都是为了学习嘛。

下载程序,命令行执行natapp.exe -authtoken=xxx ,成功后显示如下界面,你的每次请求也都会展示在界面上

Android 微信分享的openid 微信的openid怎么获取_java

第一步:进入官方文档,申请接口测试号

https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Requesting_an_API_Test_Account.html
  • appID是公众号的唯一标识符
  • appsecret是你的密钥

扫描测试号二维码–>找到网页服务–>网页账号–>网页授权获取用户基本信息–>填写你的二级域名

官方文档–>微信网页开发–>网页授权

复制提供的引导链接,写上自己的appid和redirect_uri以及scope

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6640a7f5b82ebd37&redirect_uri=http://coder-zrl.nat100.top/sell/weixin/auth&response_type=code&scope=snsapi_base&state=123#wechat_redirect

第二步:获取用户code

写一个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;

@Slf4j
@RestController
@RequestMapping("/weixin")
public class WeixinController {
    @GetMapping("/auth")
    public void auth(@RequestParam("code") String code) {
        log.info("进入了auth方法");
        log.info("【获取code】code={}",code);
    }
}

第三步:通过code获取access_token

url的参数官方文档都有详细说明

https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx6640a7f5b82ebd37&secret=85cf924b23a717e12caf329150b988c9&code=CODE&grant_type=authorization_code

返回值是json数据,里面包含openid,具体每个变量什么意义官方文档也有说明

{
  "access_token":"ACCESS_TOKEN",
  "expires_in":7200,
  "refresh_token":"REFRESH_TOKEN",
  "openid":"OPENID",
  "scope":"SCOPE" 
}

使用Spring的模板进行url访问,就是在上面的controller再加上下面的代码

我写了一个WeixinDTO和上面的字段一致,用来获取openid

import com.example.sell.dto.WeixinDTO;
import com.google.gson.Gson;
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;

@Slf4j
@RestController
@RequestMapping("/weixin")
public class WeixinController {
    @GetMapping("/auth")
    public void auth(@RequestParam("code") String code) {
        log.info("进入了auth方法");
        log.info("【获取code】code={}", code);
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=APPSECRET&code=" + code + "&grant_type=authorization_code";
        //Spring提供了一个简单便捷的模板类实现java代码访问restful服务
        //get是指调用get方法,object是我们要转化成的类型
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject(url, String.class);
        log.info("response={}",response);
        Gson gson = new Gson();
        WeixinDTO data = gson.fromJson(response,WeixinDTO.class);
        log.info("openid={}",data.getOpenid());
    }
}