forked from qiniu/java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkingDemo.java
More file actions
132 lines (108 loc) · 4.69 KB
/
Copy pathLinkingDemo.java
File metadata and controls
132 lines (108 loc) · 4.69 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
128
129
130
131
132
import com.qiniu.common.QiniuException;
import com.qiniu.linking.*;
import com.qiniu.linking.model.*;
import com.qiniu.util.Auth;
import sun.security.tools.jarsigner.TimestampedSigner;
import java.security.Timestamp;
import java.util.Date;
public class LinkingDemo {
final static String testAk = "ak";
final static String testSk = "sk";
final static String testAppid = "appid";
final static String testHost = "http://linking.qiniuapi.com";
final static String testDeviceName1 = "test1";
final static String testDeviceName2 = "test2";
public static void main(String[] args) {
Auth auth = Auth.create(testAk,testSk);
LinkingDeviceManager deviceManager = new LinkingDeviceManager(auth,testHost);
try{
//创建设备
deviceManager.createDevice(testAppid,testDeviceName1);
} catch (QiniuException e){
System.out.println(e.error());
}
try{
//添加dak
DeviceKey[] keys = deviceManager.addDeviceKey(testAppid,testDeviceName1);
System.out.println(keys[0].getAccessKey());
System.out.println(keys[0].getSecretKey());
}catch (QiniuException e){
System.out.println(e.error());
}
try{
//查询设备
DeviceKey[] keys = deviceManager.queryDeviceKey(testAppid,testDeviceName1);
if (keys.length==1){
throw new QiniuException(new Exception(),"expect one length");
}
//删除设备
deviceManager.deleteDeviceKey(testAppid,testDeviceName1,keys[0].getAccessKey());
keys = deviceManager.queryDeviceKey(testAppid,testDeviceName1);
if (keys.length==0){
throw new QiniuException(new Exception(),"expect zero length");
}
}catch (QiniuException e){
System.out.println(e.error());
}
try{
//列出设备
DeviceListing deviceslist = deviceManager.listDevice(testAppid,"","", 1,false);
System.out.println(deviceslist.items.length);
}catch (QiniuException e){
System.out.println(e.error());
}
try{
//修改设备字段
PatchOperation[] operations={new PatchOperation("replace","segmentExpireDays",9)};
Device device= deviceManager.updateDevice(testAppid,testDeviceName1,operations);
System.out.println(device.getSegmentExpireDays());
}catch (QiniuException e){
System.out.println(e.error());
}
try{
//查询设备在线历史记录
DeviceHistoryListing history= deviceManager.listDeviceHistory(testAppid,testDeviceName1,
0,(new Date().getTime())/1000,"",0);
}catch (QiniuException e){
System.out.println(e.error());
}
try{
//删除设备信息
deviceManager.deleteDevice(testAppid,testDeviceName1);
} catch (QiniuException e){
System.out.println(e.error());
}
try{
deviceManager.createDevice(testAppid,testDeviceName1);
deviceManager.createDevice(testAppid,testDeviceName2);
//添加dak
deviceManager.addDeviceKey(testAppid,testDeviceName1);
DeviceKey[] keys = deviceManager.queryDeviceKey(testAppid,testDeviceName1);
String dak = keys[0].getAccessKey();
//移动dak
deviceManager.cloneDeviceKey(testAppid,testDeviceName1,testDeviceName2,true, false,dak);
Device device = deviceManager.getDeviceByAccessKey(dak);
device.getDeviceName();
deviceManager.deleteDeviceKey(testAppid,testDeviceName2,dak);
String token;
//生成具有所有功能的token
String[] actions = new String[]{Auth.DTOKEN_ACTION_STATUS,Auth.DTOKEN_ACTION_VOD,Auth.DTOKEN_ACTION_TUTK};
token = auth.generateLinkingDeviceTokenWithExpires(testAppid,testDeviceName1,1000,actions);
//生成视频相关功能的token
token = auth.generateLinkingDeviceVodTokenWithExpires(testAppid,testDeviceName1,1000,actions);
//生成获取设备状态的token
token = auth.generateLinkingDeviceStatusTokenWithExpires(testAppid,testDeviceName1,1000,actions);
}catch (QiniuException e){
System.out.println(e.error());
}finally {
try{
deviceManager.deleteDevice(testAppid,testDeviceName1);
}catch (Exception ignored){
}
try{
deviceManager.deleteDevice(testAppid,testDeviceName2);
}catch (Exception ignored){
}
}
}
}