随笔-71  评论-5  文章-0  trackbacks-0
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

import com.alibaba.fastjson.JSONObject;
import com.huoniu.openapi.constant.Constant.TONGLIAN;


public class TLInterfaceService {
    
    private static final String USERNAME = "";
    private static final String PASSWORD = "";
    private static final String TENANT = "";
    private static final String GRANT_TYPE_GET = "password";    
    private static final String GRANT_TYPE_REFRESH = "refresh_token";    
    
    private static String TOKEN = null;
    private static String REFRESH_TOKEN = null;
    private static long EXPIRES_IN ;
    private static long START_DATE;

    
    public  String getToken() {
        if(TOKEN==null){
                TOKEN  = login();
        }else{
            Date date = new Date();
            if(START_DATE-date.getTime()<EXPIRES_IN-30){
                TOKEN  = refresh();
            }
            
        }
        
        return TOKEN;
    }
    
    
    public  void setToken(String token) {
        TOKEN = token;
    }


    /**
     * 登陆,获取token
     * 
@return 
     
*/
            
        public static String login(){

                HttpClient httpClient = new HttpClient();
                String URL = String.format(TONGLIAN.PREV_PATH, "");  //接口地址
                try {
                    PostMethod postMethod = new PostMethod(URL);
                    postMethod.addRequestHeader("Content-Type","application/x-www-form-urlencoded");
                    NameValuePair[] data = { 
                            new NameValuePair("username", USERNAME),
                            new NameValuePair("password", PASSWORD),
                            new NameValuePair("tenant",  TENANT),
                            new NameValuePair("grant_type", GRANT_TYPE_GET)
                             };
                    postMethod.setRequestBody(data);
                    int statusCode = httpClient.executeMethod(postMethod);
                    if(200 == statusCode){
                        String body  = postMethod.getResponseBodyAsString();
                        JSONObject json=JSONObject.parseObject(body);
                        TOKEN = json.getString("access_token");
                        REFRESH_TOKEN = json.getString("refresh_token");
                        EXPIRES_IN = Long.parseLong(json.getString("expires_in"));
                        START_DATE =  (new Date()).getTime();
                    }
                } catch (Exception e) {
                    
                }
                return TOKEN;
         }
        
        /**
         * refresh_token
         * 
@return 
         
*/
                        
        public static String refresh(){
                
            HttpClient httpClient = new HttpClient();
                String URL = String.format(TONGLIAN.PREV_PATH, ""); //接口地址
                try {
                    PostMethod postMethod = new PostMethod(URL);
                    postMethod.addRequestHeader("Content-Type","application/x-www-form-urlencoded");
                    NameValuePair[] data = { 
                            new NameValuePair("refresh_token", REFRESH_TOKEN),
                            new NameValuePair("grant_type", GRANT_TYPE_REFRESH)
                     };
                    postMethod.setRequestBody(data);
                    int statusCode = httpClient.executeMethod(postMethod);
                    
                    if(200 == statusCode){
                        String body  = postMethod.getResponseBodyAsString();
                        JSONObject json=JSONObject.parseObject(body);
                        TOKEN = json.getString("access_token");
                        REFRESH_TOKEN = json.getString("refresh_token");
                        EXPIRES_IN = Long.parseLong(json.getString("expires_in"));
                        START_DATE =  (new Date()).getTime();
                    }
                } catch (Exception e) {
                    
                }
                return TOKEN;
         }

}
posted on 2015-08-31 09:54 藤本蔷薇 阅读(279) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: