腾讯企业邮箱单点登录 php,腾讯企业邮箱集成的坑

这篇博客主要讨论了腾讯企业邮箱API在使用过程中遇到的一些问题,包括官方文档的错误、未读邮件数获取的权限问题以及HTTPS证书不一致的错误。作者提供了相应的解决方案,如修改WebLogic配置、绕过证书校验以及处理HTTP请求的方法。同时,博客还展示了如何进行单点登录和获取未读邮件数的代码实现。
摘要由CSDN通过智能技术生成

腾讯企业邮箱官方api文档很简略还有一些错误和漏洞:

1. 获取token单点登录和获取未读邮件数使用的secret不一致。

此处很容易误解为最初用于获取AccessToken的corpSecret

f2e8d4d41aef?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

f2e8d4d41aef?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

2.获取未读邮件数这个功能可以被管理员停用,需要开启

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

3.获取未读邮件数的请求为post请求,官方文档是get

f2e8d4d41aef?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

4.如果部署的weblogic之类的中间件可以会出现以下问题,请求的https报错证书不一致

此处的问题主要是后台发起请求的时候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;

}

单点登录与获取未读数

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";

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值