微信授权获取用户的openid

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

获取openid的两种方式

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

注册使用内网穿透工具natapp

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

https://natapp.cn/ 购买隧道,推荐VIP_1型,然后注册二级域名,注意要购买支持微信开发的域名,填写到接口测试号的网页授权获取用户基本信息中,配置隧道的二级域名和端口号(8080)。到现在你大概花了十几块钱吧,不过没关秀,知识是无价的,都是为了学习嘛。
下载程序,命令行执行natapp.exe -authtoken=xxx ,成功后显示如下界面,你的每次请求也都会展示在界面上
在这里插入图片描述

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

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());
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

笼中小夜莺

嘿嘿嘿,请用金钱尽情地蹂躏我吧

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

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

打赏作者

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

抵扣说明:

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

余额充值