關於獲取訪問令牌的部分的說明:
天翼短信接口的開發文檔鏈接:http://open.189.cn/index.php?m=content&c=index&a=lists&catid=62
實際上獲取令牌非常簡單,就是根據短信接口的說明把接口地址和要求的參數組裝好,然后發送一個post請求,然后就能夠獲取到返回的json格式的字符串。
根據剛剛上面的短信接口開放文檔說明,我們選擇獲取令牌最簡單的方式,也就是文檔里的“應用場合二”,
應用場合二:在oAuth 2.0的標准Client Credentials授權模式(簡稱CC授權模式)下,應用可憑借自身的應用ID和應用密鑰,通過調用該接口,直接獲得無需用戶授權的AT訪問令牌(User-Independent Access Token,簡稱UIAT)。
不同於普通的AT令牌,UIAT令牌其只能用於調用無需用戶授權的開放API接口,如獲取音樂榜單等;而普通的AT令牌則既可用於調用需要用戶授權的開放API接口,又可用於調用無需用戶授權的開放接口。UIAT令牌多用於無需用戶登錄的合作應用場合,盡管其使用范圍較普通AT令牌相對受限,但二者的數據形式是基本一致的。
調用地址:https://oauth.api.189.cn/emp/oauth2/v3/access_token
請求方式:post
承載協議:https
參數:

以下代碼可以正常獲取token:
1 package com.wyl.sms; 2 3 import java.io.IOException; 4 5 import com.wyl.http.UtilHttp; 6 7 public class UtilSMS { 8 private static String APP_ID = "你自己的appid"; 9 private static String APP_SECRET = "你自己的secret"; 10 //獲取訪問令牌的接口地址 ,詳見 http://open.189.cn/index.php?m=content&c=index&a=lists&catid=62 接口文檔 11 private static String URL_GET_TOKEN = "https://oauth.api.189.cn/emp/oauth2/v3/access_token"; 12 13 14 /** 15 * 組裝參數 16 * 詳見:http://open.189.cn/index.php?m=content&c=index&a=lists&catid=62 17 * @param refresh_token 18 * @param app_id 19 * @param app_secret 20 * @param state 21 * @return 22 */ 23 @SuppressWarnings("all") 24 private static String buildPara(String app_id,String app_secret,String state){ 25 StringBuilder entity = new StringBuilder(); 26 entity.append("grant_type="+"client_credentials"); 27 entity.append("&app_id="+app_id); 28 entity.append("&app_secret="+app_secret); 29 entity.append("&state="+state); 30 return entity.toString(); 31 } 32 /** 33 * 獲取訪問令牌 34 * @return 返回電信服務器返回的json格式的token等信息 35 * @throws IOException 36 */ 37 public static String getAccessToken() throws IOException{ 38 String weburl = URL_GET_TOKEN+"?"+buildPara(APP_ID, APP_SECRET, "320"); 39 String token = UtilHttp.sendPostRequest(weburl); 40 return token; 41 } 42 /** 43 * 正常獲取到token 44 * @param args 45 * @throws IOException 46 */ 47 public static void main(String[] args) throws IOException { 48 System.out.println("token:"+getAccessToken()); 49 } 50 }
UtilHttp.java:
1 package com.wyl.http; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 import java.net.HttpURLConnection; 7 import java.net.URL; 8 9 public class UtilHttp { 10 public UtilHttp(){ 11 12 } 13 /** 14 * 根據傳入的url地址,發送post請求 15 * @param weburl 16 * @return 返回請求返回的數據,轉換成String 17 * @throws IOException 18 */ 19 public static String sendPostRequest(String weburl) throws IOException{ 20 URL url = new URL(weburl); 21 HttpURLConnection conn = (HttpURLConnection)url.openConnection(); 22 conn.setDoInput(true); 23 conn.setDoOutput(true); 24 conn.setRequestMethod("POST"); 25 conn.connect(); 26 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 27 String line; 28 StringBuilder builder = new StringBuilder(); 29 while ((line = reader.readLine())!=null) { 30 builder.append(line); 31 } 32 return (builder.toString()==null?"":builder.toString()); 33 } 34 }
,代碼執行結果:

