zoukankan      html  css  js  c++  java
  • 微信-获取openid

    第一步 首先把微信的支付流程与相关的文档熟悉一遍,具体的支付逻辑是怎么实现的,心里要有一定的路数,开发的时候一边看文档,一边写,再一边调试这是最好的选择,首先阅读微信开发文档,因为我们这次是做公众号支付的,开发公众号支付时,在统一下单接口中要求必传用户openid,而获取openid则需要您在公众平台设置获取openid的域名,只有被设置过的域名才是一个有效的获取openid的域名,否则将获取失败。
    第二步 设置一个可以获取openid的域名,这个域名也是有其设定规则的,域名设置,一定要按照规定的要求设置域名。
    第三步 根据这个域名来构建链接地址,让用户点击链接先获取用户的code,如果用户在微信客户端中访问第三方网页,公众号可以通过微信网页授权机制,来获取用户基本信息,进而实现业务逻辑。微信网页授权有两种scope方式,一种是以静默的方式获取到用户的openid,用户感觉就是直接进入了页面(一般是业务页面),这种方式是snsapi_base,简单轻巧。另一种方式是以snsapi_userinfo为scope发起的授权,以弹框的形式告知用户,是否同意获取您的基本资料,包括头像,昵称,性别,城市等基本信息(绝对的安全可靠)但这种授权需要用户手动同意,并且由于用户同意过无须关注,就可在授权后获取该用户的基本信息。 
    具体构造链接地址为:
    https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect若提示“该链接无法访问”,请检查参数是否填写错误,是否拥有scope参数对应的授权作用域权限。 appid 在自己的公众号里面获取,secrete也是同样的地方获取,redirect_uri 可自己在代码中构建,顺便可以查看code值,通过日志的形式打印出来。
    如果用户同意授权,页面将跳转至 redirect_uri/?code=CODE&state=STATE。

    code说明 : code作为换取access_token的票据,每次用户授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期。

    /**这给是手动的获取到openid  首先获取的code 通过文档上面的链接获取 * 把对应的appid 都写在请求参数里面  https://open.weixin.qq.com/connect/oauth2/authorize?       appid=wx87ef5cf6d104e0bf&redirect_uri=http://ytr.natapp1.cc/weixin/auth&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect * 接着把redirect的地址 重定向的地址写在 URL里面 然后运行系统 http://ytr.natapp1.cc/weixin/auth 会请求重定向到微信客户端地址 复制 * 复制地址在微信手机端打开  然后获取出code 但是code不使用的话会在五分钟后过期  所以我们要用code换取accesstoken * 接着使用链接: https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code * 把appid secret code 都写入参数 然后在浏览器中打开 你就能看到一个返回的json格式的数据 里面就包含了openid * 例如: * { "access_token": "aPVsASECyBtZzAQhyPVfY32OfJZdbWLy4oh462TEGWNC2cKqFEleWKfGVE50cK4eghIXhOUSeOOFoXYiO15sRcPABTwiF4DvCCFjISVfk74", "expires_in": 7200, "refresh_token": "U-AZt9ATCZrn3CqZ6MMw87V6Osx6wIJG6d3mfwtvR64xfH1YQv1qAoF0hvGL8oa45RhWO1oT8tSEROkHXduHdHyfFz6v3eHVFN3V6V8Yt5Q", "openid": "o_KA80qhQVDBvePL5rdNIgU343Co", "scope": "snsapi_userinfo" }
    以上就是完成了第一步骤,成功的获取到了用户的openid。
    我们也可以通过第二种方式(第三方SDK)获取openid。
    代码如下:
    首先是配置相关的属性包括appId, secreat 后期还会有商户证书号 证书路径等配置。
    wechat:   mpAppId: wx87ef5cf6d104e0bf   mpAppSecret: fbaaff8c3552e74edbe82b7fd8455673   mchId: 1483469312   mchKey: C5245D70627C1F8E9964D494B0735025   keyPath: /usr/local/keypath/h5.p12   notifyUrl: http://ytr.natapp1.cc/sell/pay/notify
    获取配置参数 : 

    package com.wchat.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;
     
    /**
     * 配置 appid appsecret
     * Created by yangtianrui on 17/8/14.
     */
    @Component
    public class WeChatMpConfig {
     
        @Autowired
        private WeChatAccountConfig weChatAccountConfig;
        @Bean
        public WxMpService wxMpService()
        {
            WxMpService wxMpService = new WxMpServiceImpl();
            wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
            return wxMpService;
        }
     
        @Bean
        public WxMpConfigStorage wxMpConfigStorage()
        {
            WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
            wxMpConfigStorage.setAppId(weChatAccountConfig.getMpAppId());
            wxMpConfigStorage.setSecret(weChatAccountConfig.getMpAppSecret());
            return wxMpConfigStorage;
        }
    }

    构建用户点击的URL:

    package com.wchat.controller;
     
    import com.wchat.config.WeChatMpConfig;
    import com.wchat.enums.ResultEnum;
    import com.wchat.exceptions.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.api.impl.WxMpServiceImpl;
    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;
     
    /**
     * Created by yangtianrui on 17/8/14.
     */
    @Controller
    @RequestMapping("/wechat")
    @Slf4j
    public class WeChatController {
     
        @Autowired
        private WxMpService wxMpService;
        @GetMapping("/authorize")
        public String authorize(@RequestParam("returnUrl") String returnUrl)
        {
            String url ="http://ytr.natapp1.cc/sell/wechat/userinfo";
            // 该地址就是 提跳转到微信客户端打开的链接
            String resultUrl=  wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAUTH2_SCOPE_USER_INFO, URLEncoder.encode(returnUrl));
            log.info("[微信网页授权]获取resulr={}",resultUrl);
     
            return "redirect:"+resultUrl;
        }
        @GetMapping("/userinfo")
        public String userInfo(@RequestParam("code")String code,
                             @RequestParam("state")String returnUrl)
        {
            WxMpOAuth2AccessToken wxMpOAuth2AccessToken = new WxMpOAuth2AccessToken();
            try {
                wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
            }catch (WxErrorException e){
                log.error("[微信网页授权]{}",e);
                throw new SellException(ResultEnum.WX_MP_ERROR.getCode(),e.getError().getErrorMsg());
            }
            String openid = wxMpOAuth2AccessToken.getOpenId();
            log.info("openid={}",openid);
            return "redirect:"+returnUrl+"?openid="+openid;
        }
     
    }

    此时只要访问:ytr.natapp1.cc/sell/wechat/authorize?returnUrl=www.baidu.com  类似这个模版的路径即可获取用户的openid。

    参考:https://blog.csdn.net/hanerer1314/article/details/77387154

  • 相关阅读:
    node.js之Cookie
    jQuery和js之Cookie实现
    StringRedisTemplate操作Redis
    Could not get a resource from the pool 错误解决
    tableLayoutPanel 列宽度设置
    KRBTabControl(中文)Windows选项卡控件
    KRBTabControl
    Deferred content load was not performed. To provide the content, subscribe to the View's QueryControl event
    where(泛型类型约束)
    ExportAsFixedFormat Visio文件另存为其他几种格式的处理
  • 原文地址:https://www.cnblogs.com/yrjns/p/11233600.html
Copyright © 2011-2022 走看看