-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlark_api.js
120 lines (112 loc) · 4.3 KB
/
lark_api.js
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
// const timeoutPromise = new Promise((resolve, reject) => {
// setTimeout(() => {
// // reject({
// resolve({
// message: '获取授权超时'
// });
// }, 3000);
// });
// const timeoutLoginPromise = new Promise((resolve, reject) => {
// setTimeout(() => {
// // reject({
// resolve({
// message: 'login timeou登录超时'
// });
// }, 3000);
// });
function ByteDanceAPI_init() {
var app_id = "cli_a0fd2ee67578d00e";
var app_secret = "84LW4IiPknLhuaIg2jlragn5HuFrquhV";
/**
* 计算h5sdk.config的签名参数
*
* @param jsticket 之前获取的jsticket
* @param nonceStr 随机字符串
* @param timeStamp 当前时间戳
* @param url 调用h5sdk.config的当前页面URL
* @return
*/
function genSignature(jsticket, nonceStr, timeStamp, url) {
const verifyStr = `jsapi_ticket=${jsticket}&noncestr=${nonceStr}×tamp=${timeStamp}&url=${url}`;
return sha1(verifyStr);
}
//生成长度为length的随机字符串
function generateRdStr(length) {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < length; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
var data = {
"app_id": app_id,
"app_secret": app_secret
};
fetch("https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/",{
method: 'POST',
mode: "cors",
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
body: JSON.stringify(data)
})
.then(response => response.json())
.then(json => {
alert(JSON.stringify(json));
if(json.code == "0"){
var tenant_access_token = json.tenant_access_token;
fetch("https://open.feishu.cn/open-apis/jssdk/ticket/get",{
method: 'POST',
mode: "cors",
headers: {
"Content-type": "application/json; charset=utf-8",
"Authorization": "Bearer " + tenant_access_token
}
})
.then(response => response.json())
.then(json => {
alert(JSON.stringify(json));
var ticket = json.data.ticket;
var nonceStr = generateRdStr(16);
var timestamp = new Date().getTime();
var my_url = "http://www.skylight.xin";
var signature = genSignature(ticket, nonceStr, timestamp, my_url);
alert(JSON.stringify(ticket));
window.h5sdk.config({
appId: app_id, // 必填,应用ID
timestamp: timestamp, // 必填,生成签名的时间戳,ms级
nonceStr: nonceStr, // 必填,生成签名的随机串
signature: signature, // 必填,签名
jsApiList: [
'biz.user.getUserInfo',
'device.health.getStepCount',
'biz.user.openDetail',
'biz.contact.open',
'device.base.getSystemInfo',
'biz.util.getClipboardInfo',
'biz.util.openDocument',
'biz.util.downloadFile',
'device.geolocation.get',
'device.geolocation.start',
'device.geolocation.stop',
'biz.user.getUserInfoEx',
'device.connection.getNetworkType'
], // 必填,需要使用的jsapi列表。
onSuccess: function (result) {
alert("成功调用飞书API:" + JSON.stringify(result));
//成功回调
},
onFail: function () {
alert("无法注册飞书API");
//失败回调
}
});
})
.catch(err => alert('无法使用飞书API,获取ticket失败:' + JSON.stringify(err)));
}else{
alert(json.msg);
}
})
.catch(err => alert('无法使用飞书API,获取token失败:' + JSON.stringify(err)));
window.h5sdk.error(error => alert("飞书API鉴权失败:" + JSON.stringify(error)));
}