-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlist.php
79 lines (70 loc) · 2.51 KB
/
list.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
/**
* Library Requirements
*
* 1. Install composer (https://getcomposer.org)
* 2. On the command line, change to this directory (api-samples/php)
* 3. Require the google/apiclient library
* $ composer require google/apiclient:~2.0
*/
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';
$token_json = file_get_contents($TOKEN_FILE);
$client = new Google_Client();
$client->setClientId($OAUTH_FILE['OAUTH2_CLIENT_ID']);
$client->setClientSecret($OAUTH_FILE['OAUTH2_CLIENT_SECRET']);
$client->addScope('https://www.googleapis.com/auth/youtube');
$client->setAccessType('offline');
$client->setAccessToken($token_json);
if ($client->getAccessToken())
{
if($client->isAccessTokenExpired())
{
$newToken = json_decode($client->getAccessToken());
$client->refreshToken($newToken->refresh_token);
$newToken = json_decode($client->getAccessToken());
$access_token = $newToken->access_token;
$token_json = $client->getAccessToken();
file_put_contents($TOKEN_FILE, $token_json);
}
$youtube = new Google_Service_YouTube($client);
$channelsResponse = $youtube->channels->listChannels('contentDetails', array(
'mine' => 'true',
));
$result = array();
foreach ($channelsResponse['items'] as $channel)
{
// Extract the unique playlist ID that identifies the list of videos
// uploaded to the channel, and then call the playlistItems.list method
// to retrieve that list.
$uploadsListId = $channel['contentDetails']['relatedPlaylists']['uploads'];
$playlistItemsResponse = $youtube->playlistItems->listPlaylistItems('snippet', array(
'playlistId' => $uploadsListId,
'maxResults' => 50
));
foreach($playlistItemsResponse as $pi)
{
/*
print_r($pi);
exit();
*/
$result[] = array
(
'title' => $pi['snippet']['title'],
'description' => $pi['snippet']['description'],
'videoId' => $pi['snippet']['resourceId']['videoId'],
'thumbnail' => $pi['snippet']['thumbnails']['default']['url'],
'date' => $pi['snippet']['publishedAt'],
'link' => "https://www.youtube.com/watch?v={$pi['snippet']['resourceId']['videoId']}"
);
}
}
echo "<pre>";
print_r($result);
echo "</pre>";
}