|
12 | 12 | * @author Ibrahim Ulukaya |
13 | 13 | */ |
14 | 14 |
|
| 15 | +/** |
| 16 | + * Library Requirements |
| 17 | + * |
| 18 | + * 1. Install composer (https://getcomposer.org) |
| 19 | + * 2. On the command line, change to this directory (api-samples/php) |
| 20 | + * 3. Require the google/apiclient library |
| 21 | + * $ composer require google/apiclient:~2.0 |
| 22 | + */ |
| 23 | +if (!file_exists(__DIR__ . '/vendor/autoload.php')) { |
| 24 | + throw new \Exception('please run "composer require google/apiclient:~2.0" in "' . __DIR__ .'"'); |
| 25 | +} |
| 26 | + |
| 27 | +require_once __DIR__ . '/vendor/autoload.php'; |
| 28 | +session_start(); |
| 29 | + |
15 | 30 | $htmlBody = <<<END |
16 | | -<form method="GET"> |
| 31 | +<form method="POST" enctype="multipart/form-data"> |
17 | 32 | <div> |
18 | 33 | Action: |
19 | 34 | <select id="action" name="action"> |
|
50 | 65 | </form> |
51 | 66 | END; |
52 | 67 |
|
53 | | -// Call set_include_path() as needed to point to your client library. |
54 | | - require_once 'Google/Client.php'; |
55 | | - require_once 'Google/Service/YouTube.php'; |
56 | | - session_start(); |
57 | | - |
58 | | - |
59 | 68 | /* |
60 | 69 | * You can acquire an OAuth 2.0 client ID and client secret from the |
61 | 70 | * {{ Google Cloud Console }} <{{ https://cloud.google.com/console }}> |
|
66 | 75 | $OAUTH2_CLIENT_ID = 'REPLACE_ME'; |
67 | 76 | $OAUTH2_CLIENT_SECRET = 'REPLACE_ME'; |
68 | 77 |
|
69 | | -$action = $_GET['action']; |
70 | | -$videoId = $_GET['videoId']; |
71 | | -$captionFile = $_GET['captionFile']; |
72 | | -$captionName = $_GET['captionName']; |
73 | | -$captionLanguage = $_GET['captionLanguage']; |
74 | | -$captionId = $_GET['captionId']; |
75 | | - |
76 | 78 | $client = new Google_Client(); |
77 | 79 | $client->setClientId($OAUTH2_CLIENT_ID); |
78 | 80 | $client->setClientSecret($OAUTH2_CLIENT_SECRET); |
|
89 | 91 | // Define an object that will be used to make all API requests. |
90 | 92 | $youtube = new Google_Service_YouTube($client); |
91 | 93 |
|
| 94 | +// Check if an auth token exists for the required scopes |
| 95 | +$tokenSessionKey = 'token-' . $client->prepareScopes(); |
92 | 96 | if (isset($_GET['code'])) { |
93 | 97 | if (strval($_SESSION['state']) !== strval($_GET['state'])) { |
94 | 98 | die('The session state did not match.'); |
95 | 99 | } |
96 | 100 |
|
97 | 101 | $client->authenticate($_GET['code']); |
98 | | - $_SESSION['token'] = $client->getAccessToken(); |
| 102 | + $_SESSION[$tokenSessionKey] = $client->getAccessToken(); |
99 | 103 | header('Location: ' . $redirect); |
100 | 104 | } |
101 | 105 |
|
102 | | -if (isset($_SESSION['token'])) { |
103 | | - $client->setAccessToken($_SESSION['token']); |
| 106 | +if (isset($_SESSION[$tokenSessionKey])) { |
| 107 | + $client->setAccessToken($_SESSION[$tokenSessionKey]); |
104 | 108 | } |
105 | 109 |
|
106 | 110 | // Check to ensure that the access token was successfully acquired. |
107 | 111 | if ($client->getAccessToken()) { |
108 | 112 | // This code executes if the user enters an action in the form |
109 | 113 | // and submits the form. Otherwise, the page displays the form above. |
110 | | - if ($_GET['action']) { |
| 114 | + if ($_SERVER['REQUEST_METHOD'] == 'POST') { |
| 115 | + $videoId = isset($_POST['videoId']) ? $_POST['videoId'] : null; |
| 116 | + $captionFile = isset($_FILES['captionFile']) ? $_FILES['captionFile']['tmp_name'] : null; |
| 117 | + $captionName = isset($_POST['captionName']) ? $_POST['captionName'] : null; |
| 118 | + $captionLanguage = isset($_POST['captionLanguage']) ? $_POST['captionLanguage'] : null; |
| 119 | + $captionId = isset($_POST['captionId']) ? $_POST['captionId'] : null; |
111 | 120 | try { |
112 | | - switch ($action) { |
| 121 | + switch ($_POST['action']) { |
113 | 122 | case 'upload': |
114 | 123 | uploadCaption($youtube, $client, $videoId, $captionFile, |
115 | 124 | $captionName, $captionLanguage, $htmlBody); |
|
150 | 159 | htmlspecialchars($e->getMessage())); |
151 | 160 | } |
152 | 161 | } |
153 | | - $_SESSION['token'] = $client->getAccessToken(); |
| 162 | + $_SESSION[$tokenSessionKey] = $client->getAccessToken(); |
| 163 | +} elseif ($OAUTH2_CLIENT_ID == 'REPLACE_ME') { |
| 164 | + $htmlBody = <<<END |
| 165 | + <h3>Client Credentials Required</h3> |
| 166 | + <p> |
| 167 | + You need to set <code>\$OAUTH2_CLIENT_ID</code> and |
| 168 | + <code>\$OAUTH2_CLIENT_ID</code> before proceeding. |
| 169 | + <p> |
| 170 | +END; |
154 | 171 | } else { |
155 | 172 | // If the user hasn't authorized the app, initiate the OAuth flow |
156 | 173 | $state = mt_rand(); |
|
0 commit comments