初始化微信企业号秘钥

  直接代码

xml 

  
InitGetTokenServlet
com.wx.qy.servlet.InitGetTokenServlet



appid
【你的appid】


appsecret
【你的appsecret】

0

java                  

package com.wx.qy.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import com.wx.qy.util.TokenThread;

public class InitGetTokenServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;

public void init() throws ServletException {
// //  获取web.xml中配置的参数
TokenThread.appid = getInitParameter("appid");
TokenThread.appsecret = getInitParameter("appsecret");



//未配置appid、appsecret时给出提示
if ("".equals(TokenThread.appid) || "".equals(TokenThread.appsecret)) {
System.out.println("appid and appsecre配置错误,请仔细检查!");
} else {
// 启动定时获取access_token的线程
new Thread(new TokenThread()).start();
}
}
}



package com.wx.qy.util;




import java.text.SimpleDateFormat;
import java.util.Date;


import com.wx.qy.model.token.AccessToken;
import com.wx.qy.model.token.JsApiTicket;




/**
 * 
 * 定义线程获取微信token
 *
 */
public class TokenThread implements Runnable 
{
// 第三方用户唯一凭证
public static String appid = "";
// 第三方用户唯一凭证密钥
public static String appsecret = "";
//Js临时票据
public static JsApiTicket jsapiTicket = null;
//访问令牌
public static AccessToken accessToken = null;


public void run() {
while (true) {
try {
accessToken = WeixinUtil.getAccessToken(ParamesAPI.corpId, ParamesAPI.secret);
if (null != accessToken) {
jsapiTicket=WeixinUtil.getJsapiTicket(accessToken.getToken());
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:dd");  
String sd = sdf.format(new Date());  
System.out.println("【"+sd+"】获取access_token成功, token:{}"+ accessToken.getToken()+"有效时长{}秒"+accessToken.getExpiresIn());
System.out.println("【"+sd+"】获取jsapiTicket成功,jsapiTicket:{}"+jsapiTicket.getJsapiTicket() +"时间" +jsapiTicket.getExpiresIn());
// 休眠7000秒
Thread.sleep((accessToken.getExpiresIn() - 200) * 1000);

} else {
// 如果access_token为null,60秒后再获取
Thread.sleep(60 * 1000);
}
} catch (InterruptedException e) {
try {
Thread.sleep(60 * 1000);
} catch (InterruptedException e1) {
System.out.println(e1);
}
System.out.println(e);
}
}
}

public static AccessToken getAccessToken() {
return accessToken;
}


public static void setAccessToken(AccessToken accessToken) {
TokenThread.accessToken = accessToken;
}


public static JsApiTicket getJsapiTicket() {
return jsapiTicket;
}


public static void setJsapiTicket(JsApiTicket jsapiTicket) {
TokenThread.jsapiTicket = jsapiTicket;
}
}


public class JsApiTicket {
//临时票据
private String jsapiTicket;
//有效时间
private int expiresIn;
    //签名
private String signature;
//生成签名的随机串
private String nonceStr;
//生成签名的时间戳
private String timestamp;
//企业号corpid
private String  appId;

}


public class AccessToken {
/**
* 获取到的凭证
*/
private String token;
/**
* 凭证有效时间,单位:秒
*/
private int expiresIn;

}



/**
*  获取access_token的接口地址(GET)   
*/
public final static String access_token_url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=CorpID&corpsecret=SECRET";  
 
/** 
* 获取access_token 
*  
* @param CorpID 企业Id 
* @param SECRET 管理组的凭证密钥,每个secret代表了对应用、通讯录、接口的不同权限;不同的管理组拥有不同的secret 
* @return 
*/  
public static AccessToken getAccessToken(String corpID, String secret) {  
   AccessToken accessToken = null;  
 
   String requestUrl = access_token_url.replace("CorpID", corpID).replace("SECRET", secret);  
   JSONObject jsonObject = HttpRequest(requestUrl, "GET", null);  
   // 如果请求成功  
   if (null != jsonObject) {  
       try {  
           accessToken = new AccessToken();  
           accessToken.setToken(jsonObject.getString("access_token"));  
           accessToken.setExpiresIn(jsonObject.getInt("expires_in"));
           System.out.println("获取token成功:"+jsonObject.getString("access_token")+"————"+jsonObject.getInt("expires_in"));
       } catch (Exception e) {  
           accessToken = null;  
           // 获取token失败  
           String error = String.format("获取token失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));  
           System.out.println(error);
       }  
   }  
   return accessToken;  
}


/**
*  获取access_token的接口地址(GET)   
*/
public final static String GET_JS_ACCESS_TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=ACCESS_TOKE";
/**
* 获取jsapiTicket
* @param access_token
* @return
*/
public static JsApiTicket getJsapiTicket(String access_token){
JsApiTicket jsapiTicket=null; 
try {
String requestUrl = "";
requestUrl = GET_JS_ACCESS_TOKEN_URL.replace("ACCESS_TOKE", access_token);
JSONObject jsonObject  = HttpRequest(requestUrl,"GET",null);

// 如果请求成功  
   if (null != jsonObject && jsonObject.containsKey("ticket") ) {  
       try {  
        jsapiTicket = new JsApiTicket();  
        jsapiTicket.setJsapiTicket(jsonObject.getString("ticket"));
        jsapiTicket.setExpiresIn(jsonObject.getInt("expires_in"));
           System.out.println("获取JsapiTicket成功:"+jsapiTicket+"————有效时间:"+jsonObject.getInt("expires_in"));
       } catch (Exception e) {  
        jsapiTicket = null;  
           // 获取token失败  
           String error = String.format("获取JsapiTicket失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));  
           System.out.println(error);
       }  
   }  
} catch (Exception e) {
e.printStackTrace();
}
return jsapiTicket;






你可能感兴趣的:(微信企业号)