應公司要求在ERP平台的OA系統上顯示企業郵箱的未讀郵件數量,並且實現單點登錄.也就是點擊數字可以直接進入騰訊企業郵箱不用登錄.既然用到這個API肯定大家也都有騰訊企業郵箱OpenApi協議v1.4文檔了.我這是1.4的.
有了文檔就可以按照上面的步驟根據需要調用API就可以了.剛開始的時候拿到這個文檔都還不知道怎么做,百度下感覺效果不明顯,所以想寫一個稍微完整一點的實例可以供大家參考
首先我是用Java寫的,根據文檔需要去訪問它給的地址並攜帶正確參數才能返回我們需要的結果.
為了發送請求所以用了HttpClient
package com.platform.util.common; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import org.apache.log4j.Logger; /** * * ============================================ * 功能描述:發送POST/GET請求 ============================================ */ public class HttpXmlClient { private static Logger log = Logger.getLogger(HttpXmlClient.class); public static String post(String url, Map<String, String> params) { DefaultHttpClient httpclient = new DefaultHttpClient(); String body = null; log.info("create httppost:" + url); HttpPost post = postForm(url, params); body = invoke(httpclient, post); httpclient.getConnectionManager().shutdown(); return body; } public static String get(String url) { DefaultHttpClient httpclient = new DefaultHttpClient(); String body = null; log.info("create httppost:" + url); HttpGet get = new HttpGet(url); body = invoke(httpclient, get); httpclient.getConnectionManager().shutdown(); return body; } private static String invoke(DefaultHttpClient httpclient, HttpUriRequest httpost) { HttpResponse response = sendRequest(httpclient, httpost); String body = paseResponse(response); return body; } private static String paseResponse(HttpResponse response) { log.info("get response from http server.."); HttpEntity entity = response.getEntity(); log.info("response status: " + response.getStatusLine()); String charset = EntityUtils.getContentCharSet(entity); log.info(charset); String body = null; try { body = EntityUtils.toString(entity); log.info(body); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return body; } private static HttpResponse sendRequest(DefaultHttpClient httpclient, HttpUriRequest httpost) { log.info("execute post..."); HttpResponse response = null; try { response = httpclient.execute(httpost); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return response; } private static HttpPost postForm(String url, Map<String, String> params){ HttpPost httpost = new HttpPost(url); List<NameValuePair> nvps = new ArrayList <NameValuePair>(); Set<String> keySet = params.keySet(); for(String key : keySet) { nvps.add(new BasicNameValuePair(key, params.get(key))); } try { log.info("set utf-8 form entity to httppost"); httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return httpost; } }
上面的方法都是百度找的直接貼過來就可以用了.也不用去糾結主要是看效果對吧!其實也不難理解.
然后可以發送請求就簡單了,直接調用就好了
/** * 獲取騰訊企業郵箱未讀郵件 * @Description: * @param * @return void * @author * @since 2015-9-9 上午10:33:01 * @throws */ public void getNewCount(HttpServletRequest request,String email, HttpServletResponse response){ try { if(StringUtil.isNotNullOrBlank(email)&&email.contains("@hsjy.com")){ //String pyName = ""; /** * 將中文轉成拼音再拼接郵箱 */ //pyName = startService.getPingYin(personName); /** * 封裝獲取access_token需要的參數 */ Map<String, String> params = new HashMap<String, String>(); params.put("client_id", "管理帳號"); //管理帳號 params.put("grant_type", "client_credentials"); //授權類型 params.put("client_secret", "接口KEY"); //接口key /** * 發送給請求,獲取token */ String token = HttpXmlClient.post("https://exmail.qq.com/cgi-bin/token",params); log.info(token); /** * 將返回的字符串轉成Map,獲取access_token */ Map fromJson = JsonHelper.fromJson(token,Map.class); String access_token = (String) fromJson.get("access_token"); /** * 封裝獲取未讀郵件需要的參數 */ Map<String, String> params2 = new HashMap<String, String>(); params2.put("alias", email); //需要獲取的帳號 params2.put("access_token", access_token); //上面獲取到的access_token /** * 發送請求,獲取newcount未讀郵件 */ String mail = HttpXmlClient.post("http://openapi.exmail.qq.com:12211/openapi/mail/newcount",params2); Map json = JsonHelper.fromJson(mail,Map.class); String newCount = (String) json.get("NewCount"); log.info(newCount+"條未讀郵件"); //如果獲取的未讀郵件是0或者是null都設置為""頁面判斷不在顯示 if(newCount!=null&&!"".equals(newCount)&&!"0".equals(newCount)){ request.setAttribute("newcount", newCount); }else{ request.setAttribute("newcount", ""); } /** * 發送請求,獲取AuthKey */ String authKey = HttpXmlClient.post("http://openapi.exmail.qq.com:12211/openapi/mail/authkey",params2); log.info(authKey); //將json字符串轉成Map Map authKeyJson = JsonHelper.fromJson(authKey,Map.class); //獲取到authKeyValue String authKeyValue = (String) authKeyJson.get("auth_key"); //String authKey2 = HttpXmlClient.post("https://exmail.qq.com/cgi-bin/login?fun=bizopenssologin&method=bizauth&agent=hsjyadmin&user="+pyName+"@hsjy.com"+"&ticket="+authKeyValue,params2); /** * 設置頁面騰訊企業郵箱單點登錄頁面需要的參數 */ request.setAttribute("pyName", email); request.setAttribute("authKeyValue", authKeyValue);
/*try { PrintWriter writer = response.getWriter(); writer.write(authKey2); } catch (IOException e) { e.printStackTrace(); }*/ } } catch (Exception e) { e.printStackTrace(); } }
<c:choose> <c:when test="${not empty newcount}"> <dl><dt><a href="<%=common_root%>/admin/oa/commanage/mail/manage.do?formAction=receiveBoxList"><img src="images/2.0/read4.jpg" width="36" height="35" /></a></dt><dd> 未讀信件:內部 <a href="<%=common_root%>/admin/oa/commanage/mail/manage.do?formAction=receiveBoxList"><span id="noReadMailCount"></span></a>個 外部 <a href="https://exmail.qq.com/cgi-bin/login?fun=bizopenssologin&method=bizauth&agent=hsjyadmin&user=${pyName}&ticket=${authKeyValue}">${newcount}</a>個 </dd></dl> </c:when> <c:otherwise> <dl><dt><a href="<%=common_root%>/admin/oa/commanage/mail/manage.do?formAction=receiveBoxList"><img src="images/2.0/read4.jpg" width="36" height="35" /></a></dt><dd> 未讀信件: <a href="<%=common_root%>/admin/oa/commanage/mail/manage.do?formAction=receiveBoxList"><span id="noReadMailCount"></span></a>個 </dd></dl> </c:otherwise> </c:choose>
代碼已經貼出來了,相當簡單吧.
要注意的就是在傳參數的時候只需要把獲取的access_token傳過去就好了,不要去拼接前面授權類型.
此博文僅供參考.歡迎留言交流學習.
=================================================華=麗=分=割=線(更新時間:2016/12/6)==============================================
下面有個C#的園友咨詢獲取不到auth_key的問題,今天做下更新.
上面的代碼沒問題,而是現在騰訊企業郵箱在獲取token的時候需要設置可使用此開放接口的IP,如果沒有在指定的IP內會報出100001錯誤碼.設置好了就可以了.這里設置的是外網IP不是局域網IP.也就是在百度里面搜索IP顯示的地址
設置此開放接口的IP需要用管理員登錄企業郵箱 工具箱->開放接口里面設置的.最多只能有5個