Micro-channel - Get openid

The first step is the micro-channel payment processing and related documents familiar again, pay particular logic is how to achieve, the heart must be some large ones, the development time while watching the document, while writing, then this is the best side debugging choice, first read the micro-channel development of the document, because this time we are doing the public number of payments, the development of public No. pay, unified under a single interface requirements must pass user openid, acquired openid require you to set up in a public platform to obtain openid domain name, only to be set off is a valid domain name acquisition openid domain name, otherwise it will fail to obtain.
The second step openid can get a domain name, the domain name also has its own set of rules, set the domain name must be in accordance with the requirements set domain.
The third step is to build the link address based on the domain name, allowing users to click on a link to obtain the user's code, if a user visited a third-party web page in the WeChat client, the public numbers can, to get basic user information through micro-letter web authorization mechanisms, so as to realize Business logic. There are two micro-channel pages authorized scope ways, one way is to get silently to the user's openid, users feel is a direct link to a page (usually business page), this approach is snsapi_base, simple and lightweight. Another way is to snsapi_userinfo for the scope of the authorization to initiate, inform the user in the form of bomb box, if you agree to get basic information, including basic information avatar, nickname, gender, city, etc. (absolutely safe) but such authorization It requires users to manually consent, and because the user need not agree too concerned about, you can get the basic information of the user after authorization. 
Specific configuration link address is:
https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect if prompted "can not access the link," check the parameter is complete mistake, whether it has scope parameters corresponding to the scope of the authorization privileges. appid in their public numbers get inside, secrete also get the same place, redirect_uri can build their own code, the way you can see the code value printed out in the form of logs.
If the user agrees authorization, the page will jump to redirect_uri /? Code = CODE & state = STATE.

Description code: code access_token as exchange of notes, each user will be authorized to bring the code is not the same, code can only be used once, five minutes is not used expire automatically.

/ ** This is a manual to get to openid acquired first code to obtain the document through the link above * the corresponding appid are written in the request parameters inside 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 * then the redirect address redirection URL address written on the inside and then run the system http: //ytr.natapp1. cc / weixin / auth requests redirected to the micro-channel client address copy * copy the address to open and then get the code, but code is not used, then expire so we use the code in exchange for accesstoken * then use the link in five minutes in the micro-channel mobile terminal: https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code * the appid secret code is written argument and you can see a return to open in the browser json data format which contains the example openid *: * { "access_token": "aPVsASECyBtZzAQhyPVfY32OfJZdbWLy4oh462TEGWNC 2cKqFEleWKfGVE50cK4eghIXhOUSeOOFoXYiO15sRcPABTwiF4DvCCFjISVfk74 "," expires_in ": 7200,"
These are completed the first step to successfully obtain the user's openid.
We can also get openid way through the second (third-party SDK).
Code is as follows:
The first is related to the property configuration including post appId, secreat there will be business certificate certification path configurations.
wechat: mpAppId: wx87ef5cf6d104e0bf mpAppSecret: fbaaff8c3552e74edbe82b7fd8455673 mchId: 1483469312 mchKey: C5245D70627C1F8E9964D494B0735025 keyPath: /usr/local/keypath/h5.p12 notifyUrl: http://ytr.natapp1.cc/sell/pay/notify
obtain configuration parameters: 

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;
    }
}

The user clicks on the URL to build:

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")
    publicThe authorize String (@RequestParam ( "returnUrl" ) String returnUrl) 
    { 
        String URL = "http://ytr.natapp1.cc/sell/wechat/userinfo" ;
         // the jump to address is the link mentioned micro channel opened on the client 
        resultUrl = String   wxMpService.oauth2buildAuthorizationUrl (URL, WxConsts.OAUTH2_SCOPE_USER_INFO, the URLEncoder.encode (returnUrl)); 
        log.info ( "[micro channel authorization page] = {} acquired resulr" , resultUrl); 
 
        return "the 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;
    }
 
}

Then just visit:? Ytr.natapp1.cc/sell/wechat/authorize returnUrl = www.baidu.com similar path to get this template user openid.

 

Reference: https://blog.csdn.net/hanerer1314/article/details/77387154

 

Guess you like

Origin www.cnblogs.com/yrjns/p/11233600.html