SoFunction
Updated on 2025-03-08

Java calls WeChat interface to implement small functions of web sharing

This article shares the specific code for java calling the WeChat interface to implement the web sharing function for your reference. The specific content is as follows

// Get access_token *Note* After actual development and testing, WeChat sharing does not support cross-domain requests, so the request to obtain access_token must be initiated from the server, otherwise access_token cannot be obtained, so the following are all server-side operations

WeChat interface descriptionReference address

Reference article:Detailed explanation of Java WeChat sharing interface development

1. WeChat Util class

public class ShareConstants {
 //WeChat access to ticket interface public static final String TICKET_URL_TEST = "/cgi-bin/ticket/getticket" ;
 public static final String type = "jsapi" ;
 //WeChat to get the interface token public static final String GET_TOKEN_URL = "/cgi-bin/token";
 public static final String grant_type = "client_credential" ;
 
}
public class WeixinUtil {
 
 private static Logger logger = () ;
 
 public static boolean signatureCheck(String token,String timeStamp,String nonce,String signature) throws Exception{
 
 List<String > list = new ArrayList<String>(3){
  public String toString(){
  return (0)+(1)+(2) ;
  }
 } ;
 
 (token) ;
 (timeStamp) ;
 (nonce) ;
 
 (list) ;
 MessageDigest md = ("SHA-1") ;
 byte[] digest = (().getBytes()) ;
 String testStr = (digest) ;
 ("token:{},timestamp:{},nonce:{},testStr:{}");
 if((())){
  return true;
 }else{
  return false ;
 }
 }
 
 public static String signature(String jsapiTicket,String nonceStr,Long timestamp,String url) throws Exception{
 
 String str = ("jsapi_ticket=%s&noncestr=%s&timestamp=%d&url=%s",
  jsapiTicket,nonceStr,timestamp,url) ;
 ("signature url:{}",str);
 MessageDigest md = ("SHA-1") ;
 byte[] digest = (()) ;
 
 String sigStr = (digest) ;
 return sigStr ;
 }
 
 public static String byteArrayToHexString(byte[] array){
 String strDigest = "" ;
 for(int i = 0 ;i<;i++){
  strDigest+=byteToHexString(array[i]) ;
 }
 return strDigest ;
 }
 
 public static String byteToHexString(byte ib){
 char[] Digit = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'} ;
 char [] ob = new char[2] ;
 ob[0] = Digit[(ib >>> 4) & 0X0F] ;
 ob[1] = Digit[ib &0X0F] ;
 String s = new String(ob) ;
 return s ;
 }
 
 
 public static String getWeiXinShareToken(String appId,String secret) throws Exception{
 Map<String,String> maps = new HashMap<String,String>() ;
 ("grant_type", ShareConstants.grant_type) ;
 ("appid",appId) ;
 ("secret",secret) ;
 try{
  String result = (ShareConstants.GET_TOKEN_URL,maps) ;
  JSONObject jsonObject = (result) ;
  String access_token = (String) ("access_token") ;
  Integer expires_in = (Integer) ("expires_in") ;
  ("getToken access_token:{},expires_in:{}",access_token,expires_in);
  if(access_token !=null && expires_in!=null && expires_in==7200)
  return access_token ;
  else
  return null ;
 }catch (Exception ex){
  ("ex:stack:{}",().toString());
  throw new Exception("get Token failed");
 }
 }
 
 public static String getJsApiTicket(String token) throws Exception{
 Map<String,String> maps = new HashMap<String,String>() ;
 ("access_token",token);
 ("type",);
 try{
  String result = (ShareConstants.TICKET_URL_TEST,maps) ;
  JSONObject jsonObject = (result) ;
  Integer errcode = (Integer) ("errcode") ;
  if(errcode==null || (errcode!=null &&errcode!=0)){
  ("get jsapiTicket is failed, errcode:{}",errcode);
  return null ;
  }else{
  String ticket = (String) ("ticket") ;
  return ticket ;
  }
 }catch (Exception ex){
  (":{}",().toString());
  throw new Exception("getJsApi Ticket is failed") ;
 }
 }
 
}

NoticeThe above () is httpclint. You can write it yourself, as long as you can send a request.

2. Controller layer

//currUrl ==The web page address that the front-end wants to share @RequestMapping(value = "/getWConfig", method = )
 @ResponseBody
 //@RequiredLogin
 public FrameResp getWConfig(@Param("currUrl") String currUrl) throws UserException, Exception {
 if (currUrl == null) {
  return buildErrorResp(ErrorCodeConst.ERROR_PARAM_ERROR);
 }
 ("get the encode currUrl {}", currUrl);
 
 String urlTmp = (currUrl, "UTF-8");
 
 ("decode currUrl {}", currUrl);
 
 Long timestamp = new Date().getTime() / 1000;
 String url = ("#")[0];
 
 WConfigResp wConfigResp = new WConfigResp();
 
 //Modify to local load sharing //()==Own appid //()==Own AppSecret String token = ((), ());
 String ticket = (token);
 if (ticket == null) {
  ("get jsApiTicketSec is failed");
  throw new Exception("get jsApi is failed");
 }
 (());
 (timestamp);
 ((10, true, true));
 
 ("appid:{},ticket:{},timestamp:{},nonceStr:{},url:{}", (), ticket, timestamp, (), url);
 
 String signature = (ticket, (), (), url);
 if (signature != null) {
  (signature);
  return buildSuccessResp(wConfigResp);
 } else {
  ("getWcConfig is failed");
  throw new Exception("error getWConfig");
 }
}

3. Custom return value class-WConfigResp

@Data
public class WConfigResp extends BaseModel{
 private String appid ;
 private Long timestamp ;
 private String nonceStr;
 private String signature ;
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.