zoukankan      html  css  js  c++  java
  • 关于中国电信天翼开放平台短信接口的一些使用

    关于获取访问令牌的部分的说明:

    天翼短信接口的开发文档链接: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 }

    ,代码执行结果:

  • 相关阅读:
    git stash回退
    基于git tag快速修复
    Go语言开发规范
    defer实现原理
    string数据结构
    关于const iota
    struct
    map数据结构底层详解
    go 函数进阶
    go 内存分配原理
  • 原文地址:https://www.cnblogs.com/Sunnor/p/6564992.html
Copyright © 2011-2022 走看看