-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline.js
67 lines (63 loc) · 1.69 KB
/
line.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
const request = require("request");
async function verifyLIFF(accessToken) {
let options = {
method: "GET",
url: `https://api.line.me/oauth2/v2.1/verify?access_token=${accessToken}`,
headers: {
"Content-Type": "application/json",
},
};
const response = await new Promise((resolve, reject) => {
request(options, function (error, response) {
if (error) reject(new Error(error));
resolve(JSON.parse(response.body));
});
});
if (response.client_id !== process.env.LINE_CLIENT_ID) {
return new Error("wrong client id");
}
return response;
}
async function verifyCode(code, redirectURI) {
let options = {
method: "POST",
url: "https://api.line.me/oauth2/v2.1/token",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
form: {
grant_type: "authorization_code",
code: code,
client_id: process.env.LINE_CLIENT_ID,
client_secret: process.env.LINE_CLIENT_SECRET,
redirect_uri: redirectURI,
},
};
return await new Promise((resolve, reject) => {
request(options, function (error, response) {
if (error) reject(new Error(error));
resolve(JSON.parse(response.body));
});
});
}
async function gerProfile(accessToken) {
let options = {
method: "POST",
url: "https://api.line.me/v2/profile",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${accessToken}`,
},
};
return await new Promise((resolve, reject) => {
request(options, function (error, response) {
if (error) reject(new Error(error));
resolve(JSON.parse(response.body));
});
});
}
module.exports = {
verifyLIFF,
verifyCode,
gerProfile,
};