騰訊企業(yè)郵箱集成的坑

騰訊企業(yè)郵箱官方api文檔很簡略還有一些錯誤和漏洞:

1. 獲取token單點登錄和獲取未讀郵件數(shù)使用的secret不一致。

此處很容易誤解為最初用于獲取AccessToken的corpSecret

2.獲取未讀郵件數(shù)這個功能可以被管理員停用,需要開啟

停用的時候返回返回?{"errcode":602005,"errmsg":"no?privilege?to?access?app"}

3.獲取未讀郵件數(shù)的請求為post請求,官方文檔是get


4.如果部署的weblogic之類的中間件可以會出現(xiàn)以下問題,請求的https報錯證書不一致

此處的問題主要是后臺發(fā)起請求的時候weblogic會去做安全校驗,

解決辦法:1.修改weblogic配置

? ? ? ? ? ? ? ? ? 2.?HttpsURLConnection 配合Handler繞開證書校驗

? ? ? ? ? ? ? ? ? 3.請求之前信任所有證書

信任證書的方法:

private static void trustALLSSLCertificates(HttpURLConnection con) throws NoSuchAlgorithmException, KeyManagementException {

? ? ? ? ((HttpsURLConnection) con).setHostnameVerifier(new HostnameVerifier() {? ?

? ? ? ? ? ? public boolean verify(String hostname, SSLSession session) {? ?

? ? ? ? ? ? ? ? return true;? ?

? ? ? ? ? ? }?

? ? ? ? });? ?

? ? ? ? // Ignore Certification? ?

? ? ? ? TrustManager ignoreCertificationTrustManger = new X509TrustManager() {? ?


? ? ? ? ? ? public void checkClientTrusted(X509Certificate certificates[], String authType) throws CertificateException {? ?


? ? ? ? ? ? }? ?


? ? ? ? ? ? public void checkServerTrusted(X509Certificate[] ax509certificate, String s) throws CertificateException {? ?


? ? ? ? ? ? }? ?


? ? ? ? ? ? public X509Certificate[] getAcceptedIssuers() {? ?

? ? ? ? ? ? ? ? return null;? ?

? ? ? ? ? ? }?

? ? ? ? };? ?

? ? ? ? // Prepare SSL Context? ?

? ? ? ? TrustManager[] tm = { ignoreCertificationTrustManger };? ?

? ? ? ? SSLContext sslContext = SSLContext.getInstance("SSL");? ?

? ? ? ? sslContext.init(null, tm, new java.security.SecureRandom());? ?


? ? ? ? // 從上述SSLContext對象中得到SSLSocketFactory對象? ?

? ? ? ? SSLSocketFactory ssf = sslContext.getSocketFactory();? ?

? ? ? ? ((HttpsURLConnection) con).setSSLSocketFactory(ssf);? ?

? ? }?

后端請求:

public String httpGetbak(String url) {

? ? ? ? BufferedReader in = null;?

? ? ? ? try {?

? ? ? ? //com.sun.net.ssl.internal.www.protocol.https.Handler

? ? ? ? ? ? URL realUrl = new URL(null,url,new sun.net.www.protocol.https.Handler());?


? ? ? ? ? ? HttpsURLConnection? connection = (HttpsURLConnection) realUrl.openConnection();?

? ? ? ? ? ? trustALLSSLCertificates(connection);

? ? ? ? ? ? connection.setRequestProperty("accept", "*/*");?

? ? ? ? ? ? connection.setRequestProperty("connection", "Keep-Alive");?

? ? ? ? ? ? connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");?

? ? ? ? ? ? connection.setConnectTimeout(5000);?

? ? ? ? ? ? connection.setReadTimeout(5000);?


? ? ? ? ? ? connection.connect();?


? ? ? ? ? ? in = new BufferedReader(new InputStreamReader(connection.getInputStream()));?

? ? ? ? ? ? StringBuffer sb = new StringBuffer();?

? ? ? ? ? ? String line;?

? ? ? ? ? ? while ((line = in.readLine()) != null) {?

? ? ? ? ? ? ? ? sb.append(line);?

? ? ? ? ? ? }?

? ? ? ? ? ? return sb.toString();?

? ? ? ? } catch (Exception e) {?

? ? ? ? ? e.printStackTrace();

? ? ? ? }?

? ? ? ? finally {?

? ? ? ? ? ? try {?

? ? ? ? ? ? ? ? if (in != null) {?

? ? ? ? ? ? ? ? ? ? in.close();?

? ? ? ? ? ? ? ? }?

? ? ? ? ? ? } catch (Exception e2) {?

? ? ? ? ? ? ? ? e2.printStackTrace();?

? ? ? ? ? ? }?

? ? ? ? }?

? ? ? ? return null;?

? ? }?

單點登錄與獲取未讀數(shù)

public String getEmailUrl(String user, String domain, String usertype){String tokenback = httpGetbak("https://api.exmail.qq.com/cgi-bin/gettoken?corpid=wm8c6ece99e54d4813&corpsecret=r8H0thugLH4ds3Jm4HAhnD_gvVtlsBIaJG4KH-5i5kW4rW_4JPF7FnOKVVa1Bc4L");JSONObject jsStr = JSONObject.fromObject(tokenback);String token =jsStr.getString("access_token");String url =" https://api.exmail.qq.com/cgi-bin/service/get_login_url?access_token="+token+"&userid="+user+"@hnust.edu.cn";String loginUrlBack = httpGetbak(url);JSONObject jsback = JSONObject.fromObject(loginUrlBack);loginUrlBack = jsback.getString("login_url");return loginUrlBack;}


public String getUnRead(String user, String domain, String usertype) throws HttpException, IOException {String email = user +"@"+domain;String tokenback = httpGetbak("https://api.exmail.qq.com/cgi-bin/gettoken?corpid=wm8c6ece99e54d4813&corpsecret=WqG1CvlXqy6nrobMIrYCSXhFe80jgoZEw6Epx3s5XIgkRlvUX9H8NkyENW4uGaBH");JSONObject jsStr = JSONObject.fromObject(tokenback);String token =jsStr.getString("access_token"); Mapparams = new HashMap();

? Date currentTime = new Date();?

? SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");?

? String dateNow = formatter.format(currentTime);

? Calendar c = Calendar.getInstance();

? c.setTime(new Date());

? ? ? ? c.add(Calendar.DATE, - 7);

? ? ? ? Date daybefore = c.getTime();

? ? ? ? String dbef = formatter.format(daybefore);

? ? ? ? params.put("type", "0");?

? ? ? ? params.put("begin_date", dateNow);?

? ? ? ? params.put("end_date", dbef);?

? ? ? ? String url = "https://api.exmail.qq.com/cgi-bin/mail/newcount?access_token="+token+"&userid="+email;

? ? PostMethod gmethod = new PostMethod(url);

? ? HttpClient httpclient = new HttpClient();

? ? HttpClientParams ps = new HttpClientParams();

? ? ps.setParameter("type", "0");

? ? ps.setParameter("begin_date", dateNow);

? ? ps.setParameter("end_date", dbef);?

? ? httpclient.setParams(ps);

? ? int responseCode = httpclient.executeMethod(gmethod);

? ? String mailContent ="";

? ? if (responseCode == HttpURLConnection.HTTP_OK) {

InputStream inputStream = gmethod.getResponseBodyAsStream();

BufferedReader br = new BufferedReader(new InputStreamReader(

inputStream, "ISO-8859-1"));

StringBuffer resBuffer = new StringBuffer();

String tempStr = "";

while ((tempStr = br.readLine()) != null) {

resBuffer.append(new String(tempStr.getBytes("ISO-8859-1"),

"UTF-8"));

}

? ? mailContent = resBuffer.toString();

? ? JSONObject Str = JSONObject.fromObject(mailContent);

String num = Str.getString("count");

return num;

? ? }

? ? ? ? return "0";

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,500評論 19 139
  • 久違的新文章發(fā)布。。我的鍋這篇文章是在寫的太長,可以慢慢看一下,偽代碼,畢竟簡書上面,望大家見諒?。。∠瓤垂俜轿臋n...
    wyatt_plus閱讀 1,709評論 0 2
  • 該文僅對于中間這種支付方式有參考價值喲 一、開發(fā)背景 在微信公眾號中,需要進(jìn)行微信支付且為微信公眾號網(wǎng)頁支付。 二...
    英文名叫夏天閱讀 2,003評論 0 7
  • 但凡世間所有,魚與熊掌不可兼得,取舍很重要!選擇很重要! 晨跑一路看到的美景只能用眼睛去看用心去感受,不能停下的腳...
    傲夏天閱讀 607評論 1 2
  • 上學(xué)期,有一次歌唱比賽,我在學(xué)生委員那報了名,是陳奕迅的歌《十年》,一開始,我特別自信,沒事回到宿舍就邊放歌邊跟著...
    心情小屋閱讀 539評論 1 3

友情鏈接更多精彩內(nèi)容