# POA

POA(全称:platform open api),主要是方便开放性的api管理共享。本文只介绍作为api消费方,如何通过Client Id、Client Secret以及Scope去换取Access Token;关于API消费方更多内容点击此处 (opens new window)

# 获取access token

使用之前获得的Client Id、Client Secret以及之前申请的Scope去换取Access Token

POST /oauth2/token HTTP/1.1
Host: poa.xxx.edu.cn
Content-Type: application/x-www-form-urlencoded

grant_type=client_credentials&client_id=<your_client_id>&client_secret=
<your_client_secret>&scope=<your_scopes>
1
2
3
4
5
6

# 参数说明

参数 说明
grant_type 值必须是client_credentials
client_id 申请的 Client ID
client_secret 申请的 Client Secret
scope 所请求的权限,多个用英文逗号分割

# postman请求样例

传参选body->x-www-form-urlencoded

# 返回

200 OK
Content-Type: application/json

{
  "access_token": "AYjcyMzY3ZDhiNmJkNTY",
  "token_type": "bearer",
  "expires_in": 3600
}
1
2
3
4
5
6
7
8

# 返回参数说明

参数 说明
access_token 访问令牌
token_type 令牌类型,固定为bearer
expires 有效期(单位:秒)

# Demo

public class Demo {
    public static void main(String[] args) {
        //clientId、clientSecret、scope 参考上方文档 申请
        String clientId = "xxxxxxxxxxx";
        String clientSecret = "xxxxxxxxxxx";
        //申请client 授权client时的scope
        String scope = "messagecenter:v1:sendMessage,ttc:v1:writeTtc,ttc:v1:writeTransaction";
        //poa域名
        String poaDomain = "http://poa.paas.xxx.edu.cn";
        String tokenUrl = poaDomain + "/oauth2/token";
        String grantType = "client_credentials";
        String entity = String.format("grant_type=%s&client_id=%s&client_secret=%s
        &scope=%s",grantType,clientId,clientSecret, scope);

        //获取到poa token
        String accessToken =getAccessTokenObject(tokenUrl,entity).getString("access_token");

        //请求poa接口 此处示例为poa发送消息接口
        String poaUrl = PoaUtil.poaServerUrl + "/apis/messagecenter/v1/poaMessage/getMessageCategoryPageList" ;

        Map<String, Object> headers = new HashMap<String, Object>();
        //请求头携带Authorization值为 "Bearer "+token字符串
        headers.put(HttpHeaders.AUTHORIZATION, "Bearer " + PoaUtil.getAccessToken());

        Map<String, Object> parameters = new HashMap<String, Object>();
        //接口参数 示例内容略
        parameters.put("xxxx","xxxx");
        HttpResponse httpResponse =null;
        try {
          httpResponse = HttpUtils.execute(poaUrl, "POST", parameters, headers);
            //。。超时等重发略写
        } catch (Exception e) {
            e.printStackTrace();
            //todo you need
        } finally {
            if(null!=httpResponse){
                HttpUtils.close(httpResponse);
            }
        }
        //响应
        JSONObject ttcResult = parseJSONObject(httpResponse);
        //....业务系统处理响应

    }

    private static JSONObject getAccessTokenObject(String tokenUrl,String entity) {
        Map<String, Object> headers = new HashMap<String, Object>();
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        HttpResponse httpResponse = null;
        try {
            httpResponse = HttpUtils.execute(tokenUrl, "POST", null, null,
                    new HashMap<String, Object>(), headers, entity);
            //。。超时等重发略写
        } catch (Exception e) {
            e.printStackTrace();
            //todo you need
        } finally {
            if(null!=httpResponse){
                HttpUtils.close(httpResponse);
            }
        }
        JSONObject resultJsonObject = parseJSONObject(httpResponse);
        return resultJsonObject;
    }

    public static JSONObject parseJSONObject(HttpResponse httpResponse) {
        try {
            if (httpResponse != null) {
                StringBuilder entityStringBuilder = new StringBuilder();
                BufferedReader b = new BufferedReader(
                        new InputStreamReader(httpResponse.getEntity().getContent(), StandardCharsets.UTF_8), 8 * 1024);
                String line = null;
                while ((line = b.readLine()) != null) {
                    entityStringBuilder.append(line);
                }
                JSONObject resultJsonObject = JSONObject.parseObject(entityStringBuilder.toString());
                return resultJsonObject;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (UnsupportedOperationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

# 消息接口文档

详细文档地址:https://poa-docs.paas.xxx.edu.cn/messagecenter/v1/latest/

xxx 替换成学校域名就可访问,特殊情况依据现场提供地址为准

# 事务接口文档

详细文档地址:https://poa-docs.paas.xxx.edu.cn/ttc/v1/latest/#tag/NewTransactionCenter

xxx 替换成学校域名就可访问,特殊情况依据现场提供地址为准

注意:图片

# 更多关于POA

更多POA相关操作请看POA相关文档 (opens new window)