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

article/2025/9/30 6:14:10

文章目录

    • 1、创建自己的测试号
    • 2、测试号管理信息填写(注意仔细一步步对照)
    • 3、手动获取openid
    • 4、使用SDK获取openid

1、创建自己的测试号

通过申请链接:https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
微信扫码登录

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

1.测试号信息
创建登录自己的测试号之后,会自动给你 appID和appsecret,用于后面微信开发的使用

2.接口配置信息

在这里插入图片描述

  • URL配置信息参考:可以使用本地的也可以使用外网穿透方法
    https://blog.csdn.net/mys_mys/article/details/123691740
    https://blog.csdn.net/qq_41583828/article/details/82930289
    https://blog.csdn.net/qq_34096082/article/details/79985141

  • Controller测试代码:

package com.mys.controller;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;import javax.servlet.http.HttpServletRequest;/*** @author mys* @date 2022/3/25 22:07*/
@Controller
@RequestMapping("/sign")
public class GetWxSginController {private  Logger logger = LoggerFactory.getLogger(GetWxSginController.class);@ResponseBody@RequestMapping(value = "/test", method = RequestMethod.GET, produces = { "application/json;charset=utf-8" })public String getWxUserInfo(HttpServletRequest request,@RequestParam(required = false) String echostr,@RequestParam(required = false) String signature,@RequestParam(required = false) String timestamp,@RequestParam(required =false) String nonce) {try {//只需要把微信请求的 echostr, 返回给微信就可以了logger.info("测试来过===================" + echostr);logger.info("测试来过===================" + signature);logger.info("测试来过===================" + timestamp);logger.info("测试来过===================" + nonce);return echostr;} catch (Exception e) {logger.info("测试微信公众号的接口配置信息发生异常:", e);return "错误!!!";}}
}

3.JS接口安全域名

此处还有点问题,后面具体使用时再来完善,因为一个月只有三次修改机会,不敢随便修改
微信JSDK开发文档
在这里插入图片描述

4.微信扫码登录测试公众号

注意:此处一定要用使用的微信扫码,登录关注公众号才能使用
在这里插入图片描述
5.修改网页授权获取用户基本信息
在这里插入图片描述
在这里插入图片描述

3、手动获取openid

查看微信开放文档:
链接:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html

文档说明中使用步骤:
在这里插入图片描述
1.获取code

用微信访问链接需要修改的部分:
填写自己的appid,redirect_uri,scope可选snsapi_base或者snsapi_userinfo,具体作用可以查看文档,写的很详细。
在这里插入图片描述
redirect_uri与自己写的测试代码有关,就是用测试代码的访问链接,比如我的链接是:
在这里插入图片描述
写一个Controller进行测试,测试代码如下:

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,具体可查看测试代码
在这里插入图片描述
测试代码:

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);String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=自己的appid&secret=自己的secret&code="+ code + "&grant_type=authorization_code";RestTemplate restTemplate = new RestTemplate();String response = restTemplate.getForObject(url, String.class);log.info("response={}", response);}
}

运行结果:
在这里插入图片描述
复制出来的json数据如下:
在这里插入图片描述

4、使用SDK获取openid

目前已经有开源写好了一些微信开发的相关操作,我们可以直接引入maven直接使用即可,官方也写了很详细的参考文档:https://gitee.com/binary/weixin-java-tools
主要步骤:
1.配置
2.调用方法

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 {@Autowiredprivate WechatAccountConfig accountConfig;@Beanpublic WxMpService wxMpService() {WxMpService wxMpService = new WxMpServiceImpl();wxMpService.setWxMpConfigStorage(wxMpConfigStorage());return wxMpService;}@Beanpublic 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的案例,其他学习可查看文档,写的很全面。
在这里插入图片描述

package com.mys.controller;import com.mys.enums.ResultEnum;
import com.mys.exception.SellException;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;import java.net.URLEncoder;/*** @author mys* @date 2022/3/23 15:44*/
@Controller
@RequestMapping("/wechat")
@Slf4j
public class WechatController {@Autowiredprivate WxMpService wxMpService;@GetMapping("/authorize")public String authorize(@RequestParam("returnUrl") String returnUrl) {//先在application.xml中配置appid和secret,再调用方法//1.构造网页授权urlString url = "http://xqmys.natapp1.cc/sell/wechat/userInfo";//调用此方法跳转到redirectUrl便于观看结果String redirectUrl = wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAUTH2_SCOPE_BASE, URLEncoder.encode(returnUrl));return "redirect:" + redirectUrl;}@GetMapping("/userInfo")public String userInfo(@RequestParam("code") String code,@RequestParam("state") String returnUrl) {WxMpOAuth2AccessToken wxMpOAuth2AccessToken = new WxMpOAuth2AccessToken();try {//2.获取access tokenwxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);} catch (WxErrorException e) {log.error("【微信网页授权】{}", e);throw new SellException(ResultEnum.WECHAT_MP_ERROR.getCode(), e.getError().getErrorMsg());}//3.获取openidString openId = wxMpOAuth2AccessToken.getOpenId();return "redirect:" + returnUrl + "?openid=" + openId;}
}

调试运行结果:
在这里插入图片描述
很想感叹一下,看中文的注释太清晰了!
在这里插入图片描述

总结:以上便是自己通过看视频、查文章学习的步骤,花了挺长时间才搞定的。一开始听说是必须要微信公众号的服务号才能使用,但是服务号个人不能申请,必须要有营业执照之类的,最后查到可以使用微信管理的测试号,并且测试号的一些权限接口还挺多的,对于大部分开发来说是足够的。如果上述有问题,欢迎大家指正,非常感谢!


http://chatgpt.dhexx.cn/article/pxsHTp1m.shtml

相关文章

获取微信用户的openId

开发框架:struts2&#xff08;零配置&#xff09; 官方文档下载地址 https://mp.weixin.qq.com/paymch/readtemplate?tmp/business/course3_tmpl&langzh_CN PS&#xff1a;下列获取openid的代码可以在柳峰的《微信公众平台应用开发方法、技巧与案例》的第六章找到。但是…

微信公众号获取openid流程

说明 微信公众号获取openid&#xff0c;在官方文档中称为网页授权&#xff0c;授权有两种scope&#xff0c;snsapi_base和snsapi_userinfo&#xff0c;snsapi_base是静默授权&#xff0c;不需要用户同意&#xff0c;以下要说的就是静默授权。 关于网页授权的两种 scope 的区别…

微信公众号获取openId——开发阶段

1、注册测试号 微信公众平台 2、理解获取逻辑 获得微信的openid&#xff0c;需要先访问微信提供的一个网址来获取code。 再访问微信提供的另一网址从而获取openId。 两个链接分别为&#xff1a; https://open.weixin.qq.com/connect/oauth2/authorize?appidAPPID&redire…

openId -( 关注者Id )如何获取 open ID

作者&#xff1a;知乎用户 链接&#xff1a;https://www.zhihu.com/question/28101897/answer/157130437 来源&#xff1a;知乎 著作权归作者所有。商业转载请联系作者获得授权&#xff0c;非商业转载请注明出处。 程序小白&#xff0c;只懂简单代码的程度。为这个问题困扰了很…

获取微信用户openid的三种方法#ACCESS_TOKEN

本文介绍如何获得微信公众平台关注用户的基本信息&#xff0c;包括昵称、头像、性别、国家、省份、城市、语言。 在本文中&#xff0c;特别要注意的是有两个不同的Access Token&#xff0c;他们产生的方式不一样&#xff0c;一种是使用AppID和AppSecret获取的access_token&…

web元件库/axure元件库/常用web组件/常用表单/导航栏/边框/图标/日期时间选择器/评分组件/穿梭框/输入框/步骤条/

web元件库/axure元件库/常用web组件/常用表单/导航栏/边框/图标/日期时间选择器/评分组件/穿梭框/输入框/步骤条/ Axure原型演示及下载地址&#xff1a; Untitled Documenthttps://66uc7t.axshare.com axure元件库 axure元件库 axure 元件库 axure 元件库 axure 元件库 axur…

axure元件制作-常用开关

利用axure制作APP端常用图标-开关元件&#xff0c;并添加至元件库 步骤&#xff1a; 1、从iconfont 图标库中选中关闭按钮元件&#xff0c;复制至“axure”中&#xff0c;选中该“关闭按钮”元件&#xff0c;设置“交互样式-选中”&#xff0c;导入代表开启状态的元件图片&am…

Axure 自定义元件库

点击文件 -> 新建元件库 可以添加多个元件&#xff0c;并将期重命名 保存元件库 新建页面 添加元件&#xff0c;选择自建的元件库 导入后就会发现我的原件库 这样就可以使用我们自定义的元件库了

Axure9 导入元件库

将需要加载的元件库(*.rplib)放到Axure的安装目录DefaultSettings\Libraries下&#xff0c;重启Axure9即可。

Axure移动端通用元件库rplib格式包含安卓、苹果各种主流手机、平板线框图元件库、IOS系统图标、人物图标、导航和分页、表格元素、各种小图标、移动元件库、axure元件库、axure原型

Axure移动端通用元件库rplib格式包含安卓、苹果各种主流手机、平板线框图元件库、IOS系统图标、人物图标、导航和分页、表格元素、各种小图标等 移动端通用元件库、app通用元件库、数据展示、操作反馈、通用模板、数据录入、列表页、表单页、详情页、通用版布局、移动端手机模…

Axure添加官方元件库

前言 工作需要&#xff0c;领导非要说某些东西产品经理搞不定&#xff0c;要我来搞… 于是就拿起了Axure 我的版本号&#xff1a; 添加一些自己喜欢的、常用元件库 1. 下载元件库 通过Axure工具&#xff0c;跳转到官方网站&#xff0c;下载自己心仪的元件库 比如我这里下…

Axure导入元件库和使用

下载元件库 vant 元件库下载: Vant - Mobile UI Components built on Vue element UI 元件库下载:https://element.eleme.cn/#/zh-CN/resource 以vant示例&#xff0c;下载完成后解压 我们看到有.rp和.rplib格式的文件 rp文件可以理解为一个别人设计好的原型作品。 rplib是原…

Axure 9元件使用

1 元件的位置操作 直接单击选择某一个元件将其拖至画布(中间区域)中,也可以通过左上角的插入菜单选择其他形状的部件。 元件的移动&#xff1a;拖住元件并在画布上松开鼠标即可&#xff1b;也可以直接在X和Y中直接输入坐标。 1.1 同时定位多个元件 按住CTRL使用鼠标单击每个元…

WEB端后台常用Axure元件库及框架模版

原型预览链接 https://www.pmdaniu.com/storages/124048/3256710583c699e3cd26bed1dfb3aa19-105941/start.html#g1&p%E4%BD%9C%E5%93%81%E6%BC%94%E7%A4%BA 软件版本&#xff1a;Axure 8/9 当前版本&#xff1a;V1.0 作品类型&#xff1a;元件库 作品格式&…

axure不显示元件库

1.系统元件库重新load 2。找到软件安装位置&#xff0c; E:\Axure RP 8\DefaultSettings\Libraries的文件&#xff1a; 元件库点击载入即可

整理了一些已分类的产品Axure元件库,助力正在找Axure组件库的同学高速成长

在我加入的一些高质量产品交流群和一些高质量交互设计交流群里经常有同学在问&#xff0c;“有没有全一点的Axure元件库呀&#xff1f;”、“大家有没有Web端元件库呀&#xff1f;”、“有没有微信小程序元件库呀&#xff1f;”等等这些问题。 重复的问题实在是太多太多了&…

Axure 元件 模板 MES系统 全套(带下载地址)

下载地址连接地址 https://download.csdn.net/download/ybitts/85141310

Axure 元件跟随鼠标拖动-仿手机APP手指拖动

上图 主要利用动态面板的拖动事件&#xff0c;让小圆球跟随移动&#xff0c;设置好边界&#xff0c;不要移动出去就可以了。

Axure元件库web组件库典藏版 (含五大类159小类组件 )

作品说明 作品页数&#xff1a;共159页 文件大小&#xff1a;11.0MB 兼容软件&#xff1a;Axure RP 8/9/10 应用领域&#xff1a;web端原型设计、桌面端原型设计 作品特色 该AxureRP web元件库&#xff0c;支持Axure RP 8/910。含5大类、159小类&#xff0c;主要包括导航…

【Axure元件库】彩色圆形图标库 810+个矢量扁平化图标

作品说明 图标数量&#xff1a;共810个 文件大小&#xff1a;6.77MB 兼容软件&#xff1a;Axure RP 8/9/10 图标说明 本图标库为多彩扁平圆形图标元件库&#xff0c;以彩色镂空设计为主&#xff0c;适用于设置项、菜单栏、导航栏等模块的原型设计&#xff0c;可应用于休闲…