分享一个最近写的腾讯邮箱集成方法:
1 package services.tencentmail.impl; 2 3 import net.sf.json.JSONObject; 4 import org.apache.commons.codec.binary.Base64; 5 import org.springframework.dao.EmptyResultDataAccessException; 6 import org.springframework.jdbc.core.JdbcTemplate; 7 import org.springframework.stereotype.Service; 8 import services.tencentmail.HttpCore; 9 import services.tencentmail.TencentMailService; 10 11 import javax.inject.Inject; 12 import java.util.Map; 13 14 @Service 15 public class TencentMailServiceImpl implements TencentMailService { 16 17 HttpCore http = new HttpCore(); 18 @Inject 19 public JdbcTemplate jdbcTemplate; 20 21 22 public String getLinkUrl(String alias,String ssoURL,String client_id,String client_secret,String url,String host,String authkeyURL,String openAPIHost) { 23 String authKey = getAuthKey(alias,client_id,client_secret,url,host,authkeyURL,openAPIHost); 24 return ssoURL + "?fun=bizopenssologin&method=bizauth&agent=" + client_id + "&user=" + alias + "&ticket=" + authKey; 25 } 26 27 public String getAlias(String userid, String mailConfgSql,String domain) { 28 String alias = null; 29 try { 30 Map<String,Object> map = jdbcTemplate.queryForMap(mailConfgSql,userid); 31 if (!map.isEmpty()) { 32 alias = map.get("YHM") + "@"+domain; 33 } 34 } catch (EmptyResultDataAccessException e) { 35 // TODO: handle exception 36 } 37 return alias; 38 } 39 public String getAuthKey(String alias,String client_id,String client_secret,String url,String host,String authkeyURL,String openAPIHost){ 40 String content = "alias="+alias; 41 String auth = "Bearer "+getToken(client_id,client_secret,url,host); 42 net.sf.json.JSONObject json = http.httpRequest(authkeyURL, auth, content, openAPIHost); 43 return json.getString("auth_key"); 44 } 45 public String getToken(String client_id, String client_secret, String url, String host) { 46 String content = "grant_type=client_credentials"; 47 String auth = getAuthorization(client_id, client_secret); 48 JSONObject jsonObject = http.httpRequestSecure(url, auth, content, host); 49 String token = jsonObject.getString("access_token"); 50 return token; 51 } 52 public String getAuthorization(String client_id, String client_secret) { 53 byte[] source = new String(client_id + ":" + client_secret).getBytes(); 54 return new String("Basic " + Base64.encodeBase64String(source)); 55 } 56 }
时间: 2024-11-08 22:37:51