-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.php
68 lines (59 loc) · 1.71 KB
/
index.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
<?php
if (!file_exists(__DIR__ . '/vendor/autoload.php'))
throw new \Exception('please run "composer require google/apiclient:~2.0" in "' . __DIR__ .'"');
require_once __DIR__ . '/vendor/autoload.php';
$OAUTH_FILE = 'oauth.json';
$OAUTH_FILE = file_get_contents($OAUTH_FILE);
$OAUTH_FILE = json_decode($OAUTH_FILE, true);
$TOKEN_FILE = 'token.json';
$client = new Google_Client();
$client->setClientId($OAUTH_FILE['OAUTH2_CLIENT_ID']);
$client->setClientSecret($OAUTH_FILE['OAUTH2_CLIENT_SECRET']);
$client->setScopes('https://www.googleapis.com/auth/youtube');
$client->setRedirectUri($OAUTH_FILE['REDIRECT']);
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$youtube = new Google_Service_YouTube($client);
if (isset($_GET['code']))
{
$client->authenticate($_GET['code']);
$token = $client->getAccessToken();
}
if (isset($token))
$client->setAccessToken($token);
// Check to ensure that the access token was successfully acquired.
if ($client->getAccessToken())
{
$token = $client->getAccessToken();
$token_json = json_encode($token, JSON_PRETTY_PRINT);
file_put_contents($TOKEN_FILE, $token_json);
}
else
{
$state = mt_rand();
$client->setState($state);
$authUrl = $client->createAuthUrl();
$htmlBody = <<<END
<h2>GET YOUTUBE API TOKEN</h2>
<h3>Authorization Required</h3>
<p>You need to <a href="$authUrl">authorise access</a> before proceeding.<p>
END;
}
?>
<!doctype html>
<html>
<head>
<title>GET YOUTUBE API TOKEN</title>
</head>
<body>
<?php if(isset($token)): ?>
TOKEN SAVED IN <strong><?php echo $TOKEN_FILE; ?></strong>:
<pre>
<?php print_r($token_json); ?>
</pre>
<?php elseif(isset($htmlBody)): ?>
<?php echo $htmlBody; ?>
<?php endif; ?>
</pre>
</body>
</html>