zoukankan      html  css  js  c++  java
  • 如何保证access_token长期有效--微信公众平台开发

    http://blog.csdn.net/qq_33556185/article/details/52758781

    import javax.servlet.ServletContext;
    
    import org.apache.log4j.Logger;
    import org.springframework.web.context.ContextLoader;
    import org.springframework.web.context.WebApplicationContext;
    
    import com.alibaba.fastjson.JSONException;
    import com.alibaba.fastjson.JSONObject;
    
    
    public class TokenUtil {
    
        private static final Logger logger = Logger.getLogger(MessageService.class);
    
        public static String getAccessToken(String appId, String appSecret,String flag) {
            WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
            ServletContext application = webApplicationContext.getServletContext();
    
            if (application.getAttribute(flag) != null) {
    
                WeixinAccessToken tempToken = (WeixinAccessToken) application.getAttribute(flag);
    
                if (System.currentTimeMillis() < tempToken.getExpirationTime()) {
                    return tempToken.getAccessToken();
                } else {
                    return getAccessTokenContent(appId, appSecret, application,flag);
                }
            }
            return getAccessTokenContent(appId, appSecret, application,flag);
    
        }
    
        private static String getAccessTokenContent(String appId, String appSecret, ServletContext application,String flag) {
            WeixinAccessToken accessTokenFromUrl = getAccessTokenFromUrl(appId, appSecret);
            String accessToken = null;
            if (accessTokenFromUrl != null) {
                application.setAttribute(flag, accessTokenFromUrl);
                accessToken = accessTokenFromUrl.getAccessToken();
                logger.info("accessToken from url");
            }
            return accessToken;
        }
    
        public static WeixinAccessToken getAccessTokenFromUrl(String appId, String appSecret) {
            String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET";
    
            requestUrl = requestUrl.replace("APPID", appId);
            requestUrl = requestUrl.replace("SECRET", appSecret);
    
            JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);
            WeixinAccessToken tokenModel = null;
            if (null != jsonObject) {
                try {
                    String access_token = jsonObject.getString("access_token");
                    int expires_in = jsonObject.getIntValue("expires_in");
                    tokenModel = new WeixinAccessToken(access_token, System.currentTimeMillis() + expires_in*1000);
                } catch (JSONException e) {
                    logger.info("获取token失败 errcode:" + jsonObject.getIntValue("errcode") + ",errmsg:"
                            + jsonObject.getString("errmsg"));
                }
            }
            return tokenModel;
        }
    }

    WeixinAccessToken

    public class WeixinAccessToken {  
        private String accessToken;  
        private long expirationTime;  
          
        public WeixinAccessToken(){  
              
        }  
      
       public WeixinAccessToken(String accessToken,long expirationTime){  
          this.accessToken=accessToken;  
          this.expirationTime=expirationTime;  
        }  
      
          public String getAccessToken() {  
            return accessToken;  
        }  
        public void setAccessToken(String accessToken) {  
            this.accessToken = accessToken;  
        }  
        public long getExpirationTime() {  
            return expirationTime;  
        }  
        public void setExpirationTime(long expirationTime) {  
            this.expirationTime = expirationTime;  
        }  
          
      
    }  
    View Code
  • 相关阅读:
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    All strings must be XML compatible: Unicode or ASCII, no NULL bytes or control characters
    记一次odoo创建新的模块时,但是在odoo web界面找不到应用的案例
    python实现格式化输出9*9乘法表
    format和urlencode的使用对比
    python字典小知识
    01
    深浅拷贝再回顾
    DRF的路由生成类的使用
  • 原文地址:https://www.cnblogs.com/newlangwen/p/7660883.html
Copyright © 2011-2022 走看看