Repository navigation
Expand file tree
/
Copy pathRawApiCall.java
More file actions
127 lines (109 loc) · 5.32 KB
/
Copy pathRawApiCall.java
File metadata and controls
127 lines (109 loc) · 5.32 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* MIT License
*
* Copyright (c) 2022 Lark Technologies Pte. Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice, shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.lark.oapi.sample.rawapi;
import com.lark.oapi.Client;
import com.lark.oapi.core.enums.AppType;
import com.lark.oapi.core.request.RequestOptions;
import com.lark.oapi.core.response.RawResponse;
import com.lark.oapi.core.token.AccessTokenType;
import com.lark.oapi.core.utils.Jsons;
import com.lark.oapi.service.ext.enums.FileTypeEnum;
import com.lark.oapi.service.ext.model.CreateFileReq;
import com.lark.oapi.service.ext.model.CreateFileReqBody;
import com.lark.oapi.service.ext.model.CreateFileResp;
import com.lark.oapi.service.im.v1.enums.MsgTypeEnum;
import com.lark.oapi.service.im.v1.model.ext.MessageText;
import java.util.HashMap;
import java.util.Map;
/**
* 原生http 调用方式
*/
public class RawApiCall {
public static void sendMsg() throws Exception {
String appId = System.getenv().get("APP_ID");
String appSecret = System.getenv().get("APP_SECRET");
// 构建client
Client client = Client.newBuilder(appId, appSecret)
.appType(AppType.SELF_BUILT) // 设置app类型,默认为自建
.logReqAtDebug(true)
.build();
// 构建http body
Map<String, Object> body = new HashMap<>();
body.put("receive_id", "ou_c245b0a7dff2725cfa2fb104f8b48b9d");
body.put("content", MessageText.newBuilder()
.atUser("ou_155184d1e73cbfb8973e5a9e698e74f2", "Tom")
.text("test content")
.build());
body.put("msg_type", MsgTypeEnum.MSG_TYPE_TEXT);
// 发起请求
RawResponse resp = client.post(
"https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=open_id"
, body
, AccessTokenType.Tenant);
// 处理结果
System.out.println(resp.getStatusCode());
System.out.println(Jsons.DEFAULT.toJson(resp.getHeaders()));
System.out.println(new String(resp.getBody()));
System.out.println(resp.getRequestID());
}
public static void getTenantToken() throws Exception {
String appId = System.getenv().get("APP_ID");
String appSecret = System.getenv().get("APP_SECRET");
// 构建client
Client client = Client.newBuilder(appId, appSecret)
.appType(AppType.SELF_BUILT) // 设置app类型,默认为自建
.logReqAtDebug(true)
.build();
// 发起请求
Map<String, Object> body = new HashMap<>();
body.put("app_id", appId);
body.put("app_secret", appSecret);
RawResponse resp = client.post(
"https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal"
, body
, AccessTokenType.None);
// 处理结果
System.out.println(resp.getStatusCode());
System.out.println(Jsons.DEFAULT.toJson(resp.getHeaders()));
System.out.println(new String(resp.getBody()));
System.out.println(resp.getRequestID());
}
public static void createFile() throws Exception {
String appId = System.getenv().get("APP_ID");
String appSecret = System.getenv().get("APP_SECRET");
// 构建client
Client client = Client.newBuilder(appId, appSecret)
.logReqAtDebug(true)
.build();
CreateFileResp resp = client.ext()
.createFile(CreateFileReq.newBuilder()
.folderToken("fldcniHf40Vcv1DoEc8SXeuA0Zd")
.body(CreateFileReqBody.newBuilder()
.title("测试哎")
.type(FileTypeEnum.DOC)
.build())
.build(), RequestOptions.newBuilder()
.userAccessToken("u-3hVKsQTwR5H9j_tQbO2GoElg0Rggl543io00glM0277v")
.build());
// 处理结果
if (!resp.success()) {
System.out.println(
String.format("%s,%s,%s", resp.getRequestId(), resp.getMsg(), resp.getCode()));
return;
}
System.out.println(Jsons.DEFAULT.toJson(resp.getData()));
System.out.println(resp.getRequestId());
}
public static void main(String arg[]) throws Exception {
sendMsg();
}
}