-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathgenerate_and_maintain_token.php
78 lines (66 loc) · 2.1 KB
/
generate_and_maintain_token.php
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
<?php
include(__DIR__ . '/config.php');
$keys = false;
if (file_exists(__DIR__ . '/auth.json')) {
$keys = json_decode(file_get_contents(__DIR__ . '/auth.json'));
}
$generate_token = true;
if ($keys) {
// validate the token
$ch = curl_init('https://id.twitch.tv/oauth2/validate');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $keys->access_token
));
$r = curl_exec($ch);
$i = curl_getinfo($ch);
curl_close($ch);
if ($i['http_code'] == 200) {
// the token appears good
$generate_token = false;
// optional to check the expires
$data = json_decode($r);
if (json_last_error() == JSON_ERROR_NONE) {
if ($data->expires_in < 3600) {
// less than an hour left
// make a new token
echo 'Token close to expire. Regenerate';
$generate_token = true;
}
} else {
echo 'Failed to parse JSON. Assume dead token';
$generate_token = true;
}
}
}
if ($generate_token) {
$ch = curl_init('https://id.twitch.tv/oauth2/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'client_id' => CLIENT_ID,
'client_secret' => CLIENT_SECRET,
'grant_type' => "client_credentials"
));
$r = curl_exec($ch);
$i = curl_getinfo($ch);
curl_close($ch);
if ($i['http_code'] == 200) {
$keys = json_decode($r);
if (json_last_error() == JSON_ERROR_NONE) {
echo 'Got token';
print_r($keys);
// store the token for next run
file_put_contents(__DIR__ . '/auth.json', $r);
} else {
echo 'Failed to parse JSON';
}
} else {
echo 'Failed with ' . $i['http_code'] . ' ' . $r;
}
} else {
echo 'Token OK';
print_r($keys);
}
// you can then go on and use $keys to make public data calls
// of load __DIR__ . '/auth.json' in another file.