This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (48 loc) · 1.72 KB
/
index.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
const axios = require('axios')
function getAuth(client_id, redirect_uri, scope_entities){
var scope = scope_entities[0];
for(var i = 1; i < scope_entities.length; i++){
scope = scope.concat(encodeURIComponent(" ") + scope_entities[i]);
}
console.log("scope: " + scope);
return `https://id.twitch.tv/oauth2/authorize?client_id=${client_id}&redirect_uri=${redirect_uri}&response_type=code&scope=${scope}`;
}
async function get_tokens(twitch_client, twitch_secret, code, redirect){
const body = new URLSearchParams({
'client_id': twitch_client,
'client_secret': twitch_secret,
'code': code,
'redirect': redirect
});
const raw = await axios.post(`https://id.twitch.tv/oauth2/token?client_id=${twitch_client}&client_secret=${twitch_secret}&code=${code}&grant_type=authorization_code&redirect_uri=${redirect}`, body);
console.log('twitch token resp: ', raw.data);
return raw;
}
async function get_user_id(token, client_id){
const raw_user_id = await axios({
url: 'https://api.twitch.tv/helix/users?',
method: "GET",
headers: {
'Authorization': `Bearer ${token}`,
'Client-Id': client_id,
}
});
return raw_user_id;
}
async function get_user_subscription(channel_id, user_id, token, client_id){
const raw_response = await axios({
url: `https://api.twitch.tv/helix/subscriptions/user?broadcaster_id=${channel_id}&user_id=${user_id}`,
method: "GET",
headers: {
'Authorization': `Bearer ${token}`,
'Client-Id': `${client_id}`,
}
})
return raw_response;
}
module.exports = {
getAuth,
get_tokens,
get_user_id,
get_user_subscription,
}