美文网首页
微信公众平台测试号管理配置

微信公众平台测试号管理配置

作者: 阿杰_96c5 | 来源:发表于2022-04-12 10:34 被阅读0次

微信公众平台 (qq.com)

概述 | 微信开放文档 (qq.com)

微信公众号测试账号页面申请测试账号

https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

第一步:填写服务器配置

URL http://*******************/wechatLogin/checkSignature

Token hello

配置授权回调页面域名

体验接口权限表 --> 更多操作网页服务 --> 网页帐号 --> 网页授权获取用户基本信息

houhouhou.nat100.top

第二步:验证消息的确来自微信服务器

    /**
     * 验证服务器地址的有效性
     */
    @Value("${wechat.TOKEN}")
    private  String TOKEN;
    //wechat:
    //  TOKEN: hello  #接口配置信息的TOKEN

    @GetMapping("/checkSignature")
    public String checkSignature(@RequestParam(value = "signature") String signature,
                                 @RequestParam(value = "timestamp") String timestamp,
                                 @RequestParam(value = "nonce") String nonce,
                                 @RequestParam(value = "echostr") String echostr){

        String[] params = new String[]{nonce,timestamp,TOKEN};
        Arrays.sort(params);
        String signatureResult = DigestUtils.sha1Hex(params[0]+params[1] + params[2]);
        //校验签名
        if(!signatureResult.equals(signature)) {
            throw new RuntimeException("signature is not the same wechat signature is " + signature + " signatureResult is " + signatureResult);
        }
        return echostr;

    }

第三步:依据接口文档实现业务逻辑

控制层

@RestController
@RequestMapping("/wechatLogin")
public class WechatLoginController {

    @Autowired
    WechatLoginService wechatLoginService;


    /**
     * 生成用户同意授权页面url
     *
     * @return
     */
    @GetMapping("/authorize")
    public String  getRequestCodeUrl() {
        return  wechatLoginService.getRequestCodeUrl();
    }

    /**
     * 微信用户登陆
     * @param code
     * @return
     */
    @GetMapping("/login")
    public AjaxResult login(@RequestParam("code") String code) {
        return  AjaxResult.success(wechatLoginService.login(code));
    }
    //
    /**
     * 验证服务器地址的有效性
     */
    private static final String TOKEN = "wl123";
    @GetMapping("/checkSignature")
    public String checkSignature(@RequestParam(value = "signature") String signature,
                                 @RequestParam(value = "timestamp") String timestamp,
                                 @RequestParam(value = "nonce") String nonce,
                                 @RequestParam(value = "echostr") String echostr){
        
        String[] params = new String[]{nonce,timestamp,TOKEN};
        Arrays.sort(params);
        String signatureResult = DigestUtils.sha1Hex(params[0]+params[1] + params[2]);
        //校验签名
        if(!signatureResult.equals(signature)) {
            throw new RuntimeException("signature is not the same wechat signature is " + signature + " signatureResult is " + signatureResult);
        }
        return echostr;

    }

}

服务层

@Configuration
public class HTTPConfig {
    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder){
        return builder.build();
    }
}



@Service
public class WechatLoginService {

    @Autowired
    RestTemplate restTemplate;


    @Value("${wechat.appid}")
    private  String appid;

    @Value("${wechat.appsecret}")
    private  String secret;

    @Value("${wechat.redirectUri}")
    private  String redirectUri;
    //redirectUri: http://********************/wechatLogin/login



    /**
     * 用户同意授权页
     * @return返回code字符串
     */
    public String  getRequestCodeUrl() {
        UriComponentsBuilder urlBuilder = UriComponentsBuilder.fromUriString("https://open.weixin.qq.com/connect/oauth2/authorize");
        urlBuilder.queryParam("appid",appid)
                .queryParam("redirect_uri", redirectUri)
                .queryParam("response_type","code")
                .queryParam("scope","snsapi_userinfo")
                .queryParam("state","xxxx_state");
        return urlBuilder.toUriString().concat("#wechat_redirect");
    }

    /**
     * 微信用户登录
     * @param code 用户同意授权返回的code
     * @return
     */
    public WxUserInfo login(@RequestParam("code") String code) {

        // 1.获取 access_token
        WXAccessToken wxAccessToken = this.getAccessToken(code);
        //2.拉取微信用户信息
        WxUserInfo wxUserInfo = this.getWxUserInfo(wxAccessToken.getAccess_token(), wxAccessToken.getOpenid());
        return wxUserInfo;

    }


    /**
     * 获取 access_token
     * @param code
     * @return
     */
    public WXAccessToken getAccessToken(@RequestParam("code") String code){
        UriComponentsBuilder urlBuilder = UriComponentsBuilder.fromUriString("https://api.weixin.qq.com/sns/oauth2/access_token");
        urlBuilder.queryParam("appid",appid)
                .queryParam("secret",secret)
                .queryParam("code",code)
                .queryParam("grant_type","authorization_code");
        URI uri = URI.create(urlBuilder.toUriString());
        System.out.println(uri.toString());
        String resultStr = restTemplate.getForObject(uri, String.class);
        System.out.println("=====================resultStr==============");
        System.out.println(resultStr);
        return JSONObject.parseObject(resultStr, WXAccessToken.class);
    }

    /**
     * 拉取微信用户信息
     * @param accessToken
     * @param openid
     * @return
     */
    public WxUserInfo getWxUserInfo(String accessToken,String openid){
        UriComponentsBuilder urlBuilder = UriComponentsBuilder.fromUriString("https://api.weixin.qq.com/sns/userinfo");
        urlBuilder.queryParam("access_token",accessToken)
                .queryParam("openid",openid)
                .queryParam("lang","zh_CN");
        URI uri = URI.create(urlBuilder.toUriString());
        String resultStr = restTemplate.getForObject(uri, String.class);
        System.out.println("=====================resultStr==============");
        System.out.println(resultStr);
        return JSONObject.parseObject(resultStr, WxUserInfo.class);
    }
}

实体类

public class WXAccessToken {
    
    /**
     * 网页授权接口调用凭证
     */
    private String access_token;
    /**
     * access_token接口调用凭证超时时间,单位(秒)
     */
    private String expires_in;
    /**
     * 用户刷新access_token
     */
    private String refresh_token;

    /**
     * 用户唯一标识
     */
    private String openid;

    /**
     * 用户授权的作用域,使用逗号(,)分隔
     */
    private String scope;
}

======================================

public class WxUserInfo {
    /**
     * 用户的唯一标识
     */
    private String openid;
    /**
     * 用户昵称
     */
    private String nickname;
    /**
     * 用户的性别,值为1时是男性,值为2时是女性,值为0时是未知
     */
    private String sex;
    /**
     * 用户个人资料填写的省份
     */
    private String province;
    /**
     * 普通用户个人资料填写的城市
     */
    private String city;
    /**
     * 国家,如中国为CN
     */
    private String country;
    /**
     * 用户头像
     */
    private String headimgurl;
    /**
     * 用户特权信息
     */
    private String[] privilege;
    /**
     * 只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段
     */
    private String unionid;
}


相关文章

  • 微信公众平台测试号管理配置

    微信公众平台 (qq.com)[https://mp.weixin.qq.com/debug/cgi-bin/sa...

  • 微信公众号测试

    4. 微信公众号测试: 1)接口配置测试 由于微信公众号需要调用微信的接口,所以我们首先需要进行调用接口配置测试。...

  • 微信测试号的申请与连接并获取微信用户信息

    1.微信测试号的申请与连接 测试号管理帮助文档 在测试号里面设置接口配置信息的URL,一经设置,微信公众号便会发请...

  • 微信公众平台

    开发文档 微信公众平台测试号申请 微信公众平台接口调试工具

  • 微信公众号后台服务 WeixinMPSaaS

    介绍 微信公众号后台服务SaaS, 支持微信公众平台(订阅号、服务号、企业号、小程序). 支持多个微信公众号配置 ...

  • ios 微信支付集成

    1. 准备 微信平台分为微信公众平台和微信开放平台,公众平台是运营微信公众号的管理系统,开放平台主要针对app、网...

  • 微信JSSDK前端应用教程

    一、配置公众号①基本配置首先,你得有一个微信公众号,然后登陆微信公众平台 https://mp.weixin.qq...

  • iOS集成微信支付

    刚集成完微信支付,总结总结 1. 准备 微信平台分为微信公众平台和微信开放平台,公众平台是运营微信公众号的管理系统...

  • 微信公众号的网页开发(用户授权、微信分享)

    1.开发前的公众平台配置 首先确定微信公众号是服务号还是订阅号(这里只介绍服务号) 1.1申请测试账号用于本地开发...

  • 微信公众号配置

    微信支付 - 商户平台 商户号 支付证书 微信公众号 appid & appscret 开发 > 基本配置 > ...

网友评论

      本文标题:微信公众平台测试号管理配置

      本文链接:https://www.haomeiwen.com/subject/hdvgsrtx.html