移动开发-腾讯邮箱集成

  分享一个最近写的腾讯邮箱集成方法:

 

 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 import javax.inject.Inject;
11 import java.util.Map;
12 
13 @Service
14 public class TencentMailServiceImpl implements TencentMailService {
15 
16     HttpCore http = new HttpCore();
17     @Inject
18     public JdbcTemplate jdbcTemplate;
19     
20     public String getLinkUrl(String alias,String ssoURL,String client_id,String client_secret,String url,String host,String authkeyURL,String openAPIHost) {
21         String authKey = getAuthKey(alias,client_id,client_secret,url,host,authkeyURL,openAPIHost);
22         return ssoURL + "?fun=bizopenssologin&method=bizauth&agent=" + client_id + "&user=" + alias + "&ticket=" + authKey;
23     }
24 
25     public String getAlias(String userid, String mailConfgSql,String domain) {
26         String alias = null;
27         try {
28             Map<String,Object> map = jdbcTemplate.queryForMap(mailConfgSql,userid);
29             if (!map.isEmpty()) {
30                 alias = map.get("YHM") + "@"+domain;
31             }
32         } catch (EmptyResultDataAccessException e) {
33             // TODO: handle exception
34         }
35         return alias;
36     }
37     public String getAuthKey(String alias,String client_id,String client_secret,String url,String host,String authkeyURL,String openAPIHost){
38         String content = "alias="+alias;
39         String auth = "Bearer "+getToken(client_id,client_secret,url,host);
40         net.sf.json.JSONObject json = http.httpRequest(authkeyURL, auth, content, openAPIHost);
41         return json.getString("auth_key");
42     }
43     public String getToken(String client_id, String client_secret, String url, String host) {
44         String content = "grant_type=client_credentials";
45         String auth = getAuthorization(client_id, client_secret);
46         JSONObject jsonObject = http.httpRequestSecure(url, auth, content, host);
47         String token = jsonObject.getString("access_token");
48         return token;
49     }
50     public String getAuthorization(String client_id, String client_secret) {
51         byte[] source = new String(client_id + ":" + client_secret).getBytes();
52         return new String("Basic " + Base64.encodeBase64String(source));
53     }
54 }

 

posted @ 2015-08-15 13:38  景麒  阅读(300)  评论(0编辑  收藏  举报