admin管理员组

文章数量:1559737

一、发送http请求的工具类


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.URL;
import java.URLConnection;


/**
 * 发送http请求工具类(get)
 * @author chenfanglin
 * @date 2015年9月24日 下午2:47:23
 */
public class HttpRequestClient {
/**
     * 向指定URL发送GET方法的请求
     * 
     * @param url
     *            发送请求的URL
     * @param param
     *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
     * @param authorization 包含 access_token 或者 加密的(client_id:client_secret)
     * @return URL 所代表远程资源的响应结果
     */
    public static String sendGet(String url,String param,String authorization) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url + "?" + param;
            URL realUrl = new URL(urlNameString);
            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("Authorization", authorization);
            // 建立实际的连接
            connection.connect();
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常!" + url+"->"+param+"->"+authorization);
            e.printStackTrace();
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }
}


二、获取企业邮箱信息的工具类

import sun.misc.BASE64Encoder;



import com.dataeyemon.CachedObjects;


/**
 * 获取腾讯企业邮箱信息
 * 
 * @author chenfanglin
 * @date 2015年9月24日 下午2:57:38
 */
public class TencentEmailUtil {
// 公司管理员账号
public static final String CLIENT_ID = "xxxx";
// key
public static final String CLIENT_SECRET = "xxxx";
// Basic
public static final String BASIC = "Basic";
// Bearer
public static final String BEARER = "Bearer";
// grant_type
public static final String GRANT_TYPE = "grant_type=client_credentials";


/**
* 将client_id 和client_secret 以BASE64 加密方式加密,即base64(client_id: client_secret),
* @return
* @author chenfanglin <br>
* @date 2015年9月24日 下午3:27:11
*/
public static String encodeEmail(){
BASE64Encoder encoder = new BASE64Encoder();
// 加密后的字符串
String encrypted = encoder.encode((CLIENT_ID+":"+CLIENT_SECRET).getBytes());
return BASIC + " " + encrypted;
}

/**
* 根据 邮箱账号获取成员信息
* @param email
* @return
* @author chenfanglin <br>
* @date 2015年9月24日 下午3:47:56
*/
public static UserInfo getUserInfoByEmail(String email) {
String token = getAccessToken().getAccess_token();
String respon=HttpRequestClient.sendGet("http://openapi.exmail.qq:12211/openapi/user/get",
        "Alias="+email, BEARER+" "+token);
UserInfo userInfo = CachedObjects.GSON.fromJson(respon, UserInfo.class);
return userInfo;
}

/**
* OAuth验证授权,获取 access_Token  
* @return
* @author chenfanglin <br>
* @date 2015年9月24日 下午3:22:43
*/
public static AccessToken getAccessToken(){
String authorization = encodeEmail();
String respon = HttpRequestClient.sendGet("https://exmail.qq/cgi-bin/token", GRANT_TYPE, authorization);
AccessToken accessToken =  CachedObjects.GSON.fromJson(respon, AccessToken.class);
return accessToken;
}

/**
* 获取所有的公司邮箱
* @return
* @author chenfanglin <br>
* @date 2015年9月24日 下午3:50:56
*/
public static CompanyEmail getEmailList(){
String token = getAccessToken().getAccess_token();
String respon=HttpRequestClient.sendGet("http://openapi.exmail.qq:12211/openapi/user/list",
            "Ver=0",BEARER+" "+token);
CompanyEmail emailList = CachedObjects.GSON.fromJson(respon, CompanyEmail.class);
return emailList;
}

/**
* 获取公司所有的部门
* @return
* @author chenfanglin <br>
* @date 2015年9月24日 下午3:58:45
*/
public static Department getDepartmentList(){
String token = getAccessToken().getAccess_token();
String respon = HttpRequestClient.sendGet("http://openapi.exmail.qq:12211/openapi/party/list",
        "partypath=",BEARER+" "+token);
Department department = CachedObjects.GSON.fromJson(respon, Department.class);
return department;
}
}

本文标签: 腾讯企业邮箱OpenAPI