-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYoutubeServiceBuilder.php
More file actions
1303 lines (1027 loc) · 44.6 KB
/
YoutubeServiceBuilder.php
File metadata and controls
1303 lines (1027 loc) · 44.6 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Arclight\ImportBundle\Utils;
use Symfony\Component\Console\Output\OutputInterface;
use Arclight\ImportBundle\Tests\Social\FacebookImageRemote;
use Arclight\ImportBundle\Tests\Social\FacebookCaptionsFile;
use Symfony\Component\Console\Output\NullOutput;
use Arclight\MMDBBundle\php\misc\DatabaseUtils;
class YouTubeServiceBuilder
{
public const DEFAULT_REDIRECT_URI = 'http://localhost:8888/app_dev.php/callback/googleoauth';
public const YOUTUBE_CLIENT_TYPE_SERVICE = "service";
public const YOUTUBE_CLIENT_TYPE_OTHER = "Other";
public const YOUTUBE_CLIENT_TYPE_WEB = "Web application";
public const SCOPE_YOUTUBE = "https://www.googleapis.com/auth/youtube";
public const SCOPE_YOUTUBE_FORCESSL = "https://www.googleapis.com/auth/youtube.force-ssl";
public const SCOPE_YOUTUBE_READONLY = "https://www.googleapis.com/auth/youtube.readonly";
public const SCOPE_YOUTUBE_UPLOAD = "https://www.googleapis.com/auth/youtube.upload";
public const SCOPE_YOUTUBE_PARTNER = "https://www.googleapis.com/auth/youtubepartner";
public const SCOPE_YOUTUBE_PARTNER_CHANNEL_AUDIT = "https://www.googleapis.com/auth/youtubepartner-channel-audit";
public const SCOPE_YOUTUBE_ANALYTICS_MONETARY_READONLY = "https://www.googleapis.com/auth/yt-analytics-monetary.readonly";
public const SCOPE_YOUTUBE_ANALYTICS_READONLY = "https://www.googleapis.com/auth/yt-analytics.readonly";
public const SCOPE_GROUP_YOUTUBE_DATA_DEVICE = "scopeGroupYoutubeDataDevice";
public const SCOPE_GROUP_YOUTUBE_DATA = "scopeGroupYoutubeData";
public const SCOPE_GROUP_YOUTUBE_REPORTS = "scopeGroupYoutubeReports";
public const SCOPE_GROUP_YOUTUBE_QUERIES = "scopeGroupYoutubeQueries";
public $youtubeScopeGroups = [
self::SCOPE_GROUP_YOUTUBE_DATA_DEVICE => [
self::SCOPE_YOUTUBE,
self::SCOPE_YOUTUBE_READONLY,
self::SCOPE_YOUTUBE_UPLOAD
],
self::SCOPE_GROUP_YOUTUBE_DATA => [
self::SCOPE_YOUTUBE,
self::SCOPE_YOUTUBE_READONLY,
self::SCOPE_YOUTUBE_UPLOAD,
self::SCOPE_YOUTUBE_FORCESSL
],
self::SCOPE_GROUP_YOUTUBE_REPORTS => [
self::SCOPE_YOUTUBE_ANALYTICS_MONETARY_READONLY,
self::SCOPE_YOUTUBE_ANALYTICS_READONLY
],
self::SCOPE_GROUP_YOUTUBE_QUERIES => [
self::SCOPE_YOUTUBE_ANALYTICS_MONETARY_READONLY,
self::SCOPE_YOUTUBE_ANALYTICS_READONLY,
self::SCOPE_YOUTUBE,
self::SCOPE_YOUTUBE_PARTNER
]
];
private $userAccountEmail;
private $projectName;
private $projectRowId;
private $ytAccountRowId;
private $userId;
private $channelId;
private $channelTitle;
/**
* @var \Doctrine\DBAL\Connection
*/
private $conn;
/**
* Optional console output interface to be used for error/debug output
*
* @var OutputInterface
*/
private $output;
/**
* Key: clientType
* Value: array containing keys: 'access_token_id', 'youtube_service'
*/
private $oauthClientYouTubeServices = [];
private $serviceAccountYouTubeService = null;
private $quotaExceeded = null;
private $uploadLimitExceeded = null;
private $isCommandLine = true;
private $redirectUri = self::DEFAULT_REDIRECT_URI;
/**
* Initializes a new <tt>YouTubeServiceBuilder</tt>.
*
* @param string $userAccountEmail Google user account email address used to log in such as 'jfmediaqa@gmail.com '
* @param string $projectName Google API project name such as 'Conversation Starters Project'
* @param string $channelTitle YouTube channel for which to build clients + services
* @param \Doctrine\DBAL\Connection $conn
* @param OutputInterface $output
*/
public function __construct($userAccountEmail, $projectName, $channelTitle, \Doctrine\DBAL\Connection $conn, OutputInterface $output = null)
{
$this->userAccountEmail = $userAccountEmail;
$this->projectName = $projectName;
$this->channelTitle = $channelTitle;
$this->conn = $conn;
$this->output = $output ?? new NullOutput();
$googleApiProject = $this->getGoogleApiProjectFromDB();
if (is_null($googleApiProject)) {
throw new \Exception("Invalid Google api project name [$projectName] for user account [$userAccountEmail]");
}
$this->projectRowId = $googleApiProject['id'];
$account = $this->getUserChannelIdFromDB();
if (is_null($account)) {
throw new \Exception("Invalid Google YouTube channel title [$channelTitle] provided for project name [$projectName] and user account [$userAccountEmail]");
}
$this->ytAccountRowId = $account['id'];
$this->userId = $account['userId'];
$this->channelId = $account['channelId'];
$this->reset();
}
public function setIsCommandLine($isCommandLine = true) {
$this->isCommandLine = $isCommandLine;
}
public function setRedirectUri($redirectUri) {
$this->redirectUri = $redirectUri;
}
public function getUserId()
{
return $this->userId;
}
public function getChannelId()
{
return $this->channelId;
}
/**
* Call this for instance if you are changing the set of 'scopes' accessible
* via a specific oauth client access token.
*/
public function forceReauthenticate()
{
$oauthClients = $this->getOauthClientsFromDB();
foreach ($oauthClients as $oauthClient) {
if ($oauthClient['type'] == self::YOUTUBE_CLIENT_TYPE_WEB) {
$accessTokenInfo['access_token'] = null;
$accessTokenInfo['refresh_token'] = null;
$accessTokenInfo['expires_in'] = null;
$accessTokenInfo['token_type'] = null;
$accessTokenInfo['created'] = null;
$this->updateOauthAccessTokenInDB($oauthClient['access_token_id'], $accessTokenInfo, [], $this->conn);
}
}
}
public function reset()
{
$this->oauthClientYouTubeServices = [];
$this->serviceAccountYouTubeService = null;
$this->pullLimitsExceededAt();
return $this;
}
public function getAuthorizedYouTubeDataApiKeyService()
{
$apiKeyInfo = $this->getApiKeyInfoFromDB();
return self::createYouTubeApiKeyService($apiKeyInfo);
}
public function getAuthorizedYouTubeDataDeviceService($forceAccessTokenRefresh = false)
{
$scopes = $this->youtubeScopeGroups[self::SCOPE_GROUP_YOUTUBE_DATA_DEVICE];
return $this->getAuthorizedYouTubeServiceForType(self::YOUTUBE_CLIENT_TYPE_OTHER, $scopes, $forceAccessTokenRefresh);
}
public function getAuthorizedYouTubeDataService($forceAccessTokenRefresh = false)
{
$scopes = $this->youtubeScopeGroups[self::SCOPE_GROUP_YOUTUBE_DATA];
return $this->getAuthorizedYouTubeServiceForType(self::YOUTUBE_CLIENT_TYPE_WEB, $scopes, $forceAccessTokenRefresh);
}
public function getAuthorizedYouTubeReportService($forceAccessTokenRefresh = false)
{
$scopes = $this->youtubeScopeGroups[self::SCOPE_GROUP_YOUTUBE_REPORTS];
return $this->getAuthorizedYouTubeServiceForType(self::YOUTUBE_CLIENT_TYPE_WEB, $scopes, $forceAccessTokenRefresh);
}
public function getAuthorizedYouTubeQueryService($forceAccessTokenRefresh = false)
{
$scopes = $this->youtubeScopeGroups[self::SCOPE_GROUP_YOUTUBE_QUERIES];
return $this->getAuthorizedYouTubeServiceForType(self::YOUTUBE_CLIENT_TYPE_WEB, $scopes, $forceAccessTokenRefresh);
}
public function getAuthorizedYouTubeServiceForName($clientName, $oauthScopes, $forceAccessTokenRefresh)
{
$oauthClient = $this->getOauthAccessTokenFromDB(
$this->userAccountEmail,
$this->projectName,
$this->channelTitle,
$clientName,
$this->conn);
$youTubeService = $this->buildOauthClientYouTubeServiceForDB($oauthClient, $oauthScopes, $forceAccessTokenRefresh);
return $youTubeService['youtube_service'];
}
public function getAuthorizedYouTubeServiceForType($clientType, $oauthScopes, $forceAccessTokenRefresh)
{
if ($clientType === self::YOUTUBE_CLIENT_TYPE_SERVICE) {
if (is_null($this->serviceAccountYouTubeService)) {
$this->serviceAccountYouTubeService = $this->buildServiceAccountYouTubeService();
}
return $this->serviceAccountYouTubeService;
}
$s = is_array($oauthScopes) ? implode(" ", $oauthScopes) : $oauthScopes;
if (!array_key_exists($clientType, $this->oauthClientYouTubeServices) ||
!array_key_exists($s, $this->oauthClientYouTubeServices[$clientType])) {
$this->output->writeln("Building oauth client youtube service for client type [$clientType] for scopes [$s]");
$this->oauthClientYouTubeServices[$clientType][$s] = $this->buildOauthClientYouTubeServiceForType($clientType, $oauthScopes, $forceAccessTokenRefresh);
if (is_null($this->oauthClientYouTubeServices[$clientType][$s])) {
throw new \Exception("Missing Google oauth client type [$clientType][$s] for channel title [$this->channelTitle] and project name [$this->projectName]");
} else if (false === $this->oauthClientYouTubeServices[$clientType][$s]) {
throw new \Exception("User authorization cancelled");
}
$yt = $this->oauthClientYouTubeServices[$clientType][$s]['youtube_service'];
if (!is_null($yt) && !empty($yt->getClient()->getAccessToken())) {
return $yt;
} else {
return null;
}
}
/**
* @var string $accessTokenId
*/
$accessTokenId = $this->oauthClientYouTubeServices[$clientType][$s]['access_token_id'];
/**
* @var \Google_Service_YouTube $youTubeService
*/
$youTubeService = $this->oauthClientYouTubeServices[$clientType][$s]['youtube_service'];
// Probably invalid clientType?
if (is_null($youTubeService)) {
return null;
}
$googleClient = $youTubeService->getClient();
$existingScopes = $googleClient->getScopes();
$missingScopes = YouTubeServiceBuilder::diffScopes($oauthScopes, $existingScopes);
if (empty($googleClient->getAccessToken()) ||
true === $googleClient->isAccessTokenExpired() ||
true === $forceAccessTokenRefresh ||
!empty($missingScopes)) {
$refreshed = false;
$oauthClient = null;
if (!empty($accessTokenId)) {
$oauthClient = $this->getOauthClientAccessTokenFromDBById($accessTokenId);
if (empty($missingScopes)) {
$this->output->writeln("Attempting to refresh already built youtube service of client type [$clientType]");
$refreshed = $this->refreshOauthClientAccessToken($oauthClient, $youTubeService);
}
}
if (false === $refreshed && $clientType === self::YOUTUBE_CLIENT_TYPE_WEB) {
if (is_null($oauthClient)) {
$clientName = $this->getOauthClientNameFromDB($clientType);
// Build just enough of $oauthClient structure to allow
// our interactive authorization method to work properly:
$oauthClient = [
'user_account_email' => $this->userAccountEmail,
'project_name' => $this->projectName,
'channel_title' => $this->channelTitle,
'oauth_client_name' => $clientName ];
}
$this->output->writeln("Attempting to authorize already built youtube service of client type [$clientType] and name [".$oauthClient['oauth_client_name']."]");
$this->oauthClientYouTubeServices[$clientType][$s]['access_token_id'] =
$this->obtainAuthorizationInteractively($oauthClient, $youTubeService);
}
}
if (empty($youTubeService->getClient()->getAccessToken()) ||
true === $youTubeService->getClient()->isAccessTokenExpired()) {
$this->output->writeln("Attempted to refresh already built youtube service, but still not authorized!");
return null;
}
$this->output->writeln("Returning previously authorized youtube service of client type [$clientType]");
return $youTubeService;
}
/**
* Builds them all for a given channel title, returning an array of
* Google_Service_YouTube using the type of service as key.
*
* @param boolean $forceAccessTokenRefresh
* @throws \Exception
* @return \Google_Service_YouTube[]
*/
public function buildYouTubeServices($forceAccessTokenRefresh = false)
{
$youTubeServices = [];
$youTubeServices[self::YOUTUBE_CLIENT_TYPE_SERVICE] = $this->buildServiceAccountYouTubeService();
$oauthClients = $this->getOauthClientsFromDB();
foreach ($oauthClients as $oauthClient) {
$this->output->writeln("Building oauth client youtube service for client type [".$oauthClient['type']."]");
// For scopes, send null to indicate that we want to use what is
// already in the DB
$accessTokenIdAndYouTubeService = $this->buildOauthClientYouTubeServiceForDB($oauthClient, $forceAccessTokenRefresh, null);
$youTubeServices[$oauthClient['type']] = $accessTokenIdAndYouTubeService['youtube_service'];
}
return $youTubeServices;
}
/**
* Authenticates, creates a google client + YouTube service based on the
* [google_api_oauth_client] table that is related to a given user account's
* [google_api_project].
*
* For clients of type
* 1. Oauth "Web application": perform a user-interactive authentication that includes (if necessary)
* choosing a matching brand account (per incoming channelTitle)
* 2. Oauth "Other": an access_token + refresh_token must already be in google_api_oauth_access_token
* 3. For service accounts, use the pre-configured service account credentials path
*
* @param string $clientType filter
* @param array $oauthScopes minimal scopes to include in an Oauth Google client (optional - may be null. Doesn't apply to service accounts)
* @param boolean $forceAccessTokenRefresh (Doesn't apply to service accounts)
* @throws \Exception
* @return \Google_Service_YouTube
*/
public function buildYouTubeService($clientType = self::YOUTUBE_CLIENT_TYPE_WEB, $oauthScopes, $forceAccessTokenRefresh = false)
{
if ($clientType == self::YOUTUBE_CLIENT_TYPE_SERVICE) {
$this->output->writeln("Building youtube service for client type [$clientType]");
return $this->buildServiceAccountYouTubeService();
}
$this->output->writeln("Building oauth client youtube service for client type [$clientType]");
$accessTokenIdAndYouTubeService = $this->buildOauthClientYouTubeServiceForType($clientType, $oauthScopes, $forceAccessTokenRefresh);
return $accessTokenIdAndYouTubeService['youtube_service'];
}
protected function buildOauthClientYouTubeServiceForType($clientType, $scopes, $forceAccessTokenRefresh = false)
{
$accessTokenIdAndYouTubeService = null;
// $candidateMissingScopes = null;
$oauthClients = $this->getOauthClientsFromDB();
foreach ($oauthClients as $oauthClient) {
if ($oauthClient['type'] != $clientType) {
continue;
}
// Commented this out, so that if scopes are different, we reauthenticate using
// the new set of scopes:
// if (!empty($oauthClient['scopes']) && !empty(YouTubeServiceBuilder::diffScopes($scopes, $oauthClient['scopes']))) {
// $candidateMissingScopes = $oauthClient;
// continue;
// }
$accessTokenIdAndYouTubeService = $this->buildOauthClientYouTubeServiceForDB($oauthClient, $scopes, $forceAccessTokenRefresh);
break;
}
if (is_null($accessTokenIdAndYouTubeService)) {
// if (!is_null($candidateMissingScopes)) {
// Clear out access token info:
// $oauthClient['access_token_id'] = null;
// $oauthClient['auth_code'] = null;
// $oauthClient['access_token'] = null;
// $oauthClient['token_type'] = null;
// $oauthClient['expires_in'] = null;
// $oauthClient['created'] = null;
// $oauthClient['refresh_token'] = null;
// $oauthClient['scopes'] = null;
//
// $accessTokenIdAndYouTubeService = $this->buildOauthClientYouTubeServiceForDB($oauthClient, $scopes, $forceAccessTokenRefresh);
// } else {
$this->output->writeln("\t No oauth client found for scopes [".implode(',', $scopes)."]. Need to set up another [$clientType] oauth client");
// }
}
return $accessTokenIdAndYouTubeService;
}
protected function buildOauthClientYouTubeServiceForDB($oauthClient, $scopes, $forceRefresh)
{
$accessTokenIdAndYouTubeService = null;
if ($oauthClient['type'] == self::YOUTUBE_CLIENT_TYPE_WEB) {
$accessTokenIdAndYouTubeService = $this->buildWebOauthClientYouTubeService($oauthClient, $scopes, $forceRefresh);
} else if ($oauthClient['type'] == self::YOUTUBE_CLIENT_TYPE_OTHER) {
$accessTokenIdAndYouTubeService = $this->buildOtherOauthClientYouTubeService($oauthClient, $scopes, $forceRefresh);
} else {
throw new \Exception("Oauth client type [".$oauthClient['type']."] not implemented!");
}
return $accessTokenIdAndYouTubeService;
}
/**
* Creates a Google client + YouTube service based on info in the
* [google_api_service_account] table that is related to the given
* user account's [google_api_project].
*
* There should only be a
* single service account configured for a given user account, which
* should include the appropriate JSON credentials file per Google
* documentation.
*
* @return NULL|\Google_Service_YouTube
*/
public function buildServiceAccountYouTubeService()
{
$serviceAccount = $this->getServiceAccountFromDB();
if (is_null($serviceAccount)) {
return null;
}
$pathToCredentials = $serviceAccount['path_to_credentials'];
putenv("GOOGLE_APPLICATION_CREDENTIALS=$pathToCredentials");
//$this->output->writeln("Putenv(GOOGLE_APPLICATION_CREDENTIALS): ".getenv("GOOGLE_APPLICATION_CREDENTIALS"));
$googleClient = new \Google_Client();
$googleClient->useApplicationDefaultCredentials();
$youTubeService = new \Google_Service_YouTube($googleClient);
$this->output->writeln("Finished creating youtube service account for name [".$serviceAccount['account_name']."] and user [".$serviceAccount['user_account']."]");
return $youTubeService;
}
protected function buildOtherOauthClientYouTubeService($oauthClient, $scopes, $forceRefresh)
{
$this->output->writeln("\t Creating [".$oauthClient['type']."] oauth youtube service for channel [".$oauthClient['channel_title']."] and client [".$oauthClient['oauth_client_name']."]");
if (!empty(YouTubeServiceBuilder::diffScopes($scopes, $oauthClient['scopes']))) {
throw new \Exception("Unable to change scopes of 'device' oauth client - existing [".implode(',', $oauthClient['scopes'])."] vs requested [".implode(',', $scopes)."]");
}
$youTubeService = YouTubeServiceBuilder::createYouTubeService($oauthClient, $this->redirectUri);
$secondsTillExpire = 0;
if (!empty($oauthClient['access_token'])) {
$secondsTillExpire = $this->calcSecondsUntilExpiration($oauthClient['created'], $oauthClient['expires_in']);
}
if (empty($oauthClient['access_token']) || 0 >= $secondsTillExpire || true == $forceRefresh) {
if (!array_key_exists('refresh_token', $oauthClient)) {
throw new \Exception("Unable to refresh our [".$oauthClient['type']."] oauth access token due to missing 'refresh_token'");
}
$this->refreshOauthClientAccessToken($oauthClient, $youTubeService);
} else {
$this->output->writeln("\t Using existing [".$oauthClient['type']."] access token with id [".$oauthClient['access_token_id']."] - hasn't expired yet (Expires in [$secondsTillExpire] seconds)");
$accessTokenInfo = $this->extractAccessTokenInfo($oauthClient);
$youTubeService->getClient()->setAccessToken($accessTokenInfo);
}
if (!is_null($youTubeService)) {
$this->output->writeln("\t Finished creating [".$oauthClient['type']."] oauth youtube service for channel [".$oauthClient['channel_title']."] and client [".$oauthClient['oauth_client_name']."]");
}
return [ 'access_token_id' => $oauthClient['access_token_id'], 'youtube_service' => $youTubeService ];
}
protected function buildWebOauthClientYouTubeService($oauthClient, $scopes, $forceRefresh)
{
$this->output->writeln("\t Creating [".$oauthClient['type']."] oauth youtube service for channel [".$oauthClient['channel_title']."] and client [".$oauthClient['oauth_client_name']."]");
$youTubeService = YouTubeServiceBuilder::createYouTubeService($oauthClient, $this->redirectUri);
$googleClient = $youTubeService->getClient();
$accessTokenId = array_key_exists('access_token_id', $oauthClient) ? $oauthClient['access_token_id'] : null;
$accessToken = $oauthClient['access_token'];
$created = $oauthClient['created'];
$expiresInSeconds = $oauthClient['expires_in'];
$refreshToken = $oauthClient['refresh_token'];
$needToAuth = false;
// Decide on scopes:
if (empty($scopes) && empty($oauthClient['scopes'])) {
// use default and reauthenticate since we don't know what was there
$googleClient->setScopes($this->youtubeScopeGroups[self::SCOPE_GROUP_YOUTUBE_DATA]);
$needToAuth = true;
$accessToken = null;
$refreshToken = null;
} else if (!empty($scopes)) {
$googleClient->setScopes($scopes);
$diff = YouTubeServiceBuilder::diffScopes($scopes, $oauthClient['scopes']);
if (!empty($diff)) {
// reauthenticate if something changed scope-wise:
$needToAuth = true;
$accessToken = null;
$refreshToken = null;
}
}
if (!is_null($accessToken)) {
$secondsTillExpire = $this->calcSecondsUntilExpiration($created, $expiresInSeconds);
if ($secondsTillExpire > 0 && false == $forceRefresh) {
$this->output->writeln("\t Using existing [".$oauthClient['type']."] access token with id [".$oauthClient['access_token_id']."] - hasn't expired yet (Expires in [$secondsTillExpire] seconds)");
$accessTokenInfo = $this->extractAccessTokenInfo($oauthClient);
$googleClient->setAccessToken($accessTokenInfo);
} else {
if ($secondsTillExpire <= 0) {
$this->output->writeln("\t Access token expired [".(-$secondsTillExpire)."] seconds ago. Getting new one...");
} else {
$this->output->writeln("\t Forced refresh of access token that was set to expire in [".$secondsTillExpire."] seconds. Getting new one...");
}
$needToAuth = true;
}
} else {
$needToAuth = true;
}
if (true === $needToAuth && !is_null($refreshToken)) {
$needToAuth = !$this->refreshOauthClientAccessToken($oauthClient, $youTubeService);
}
if (true === $needToAuth) {
$accessTokenId = $this->obtainAuthorizationInteractively($oauthClient, $youTubeService);
if (is_null($accessTokenId)) {
return false;
}
}
if (!is_null($youTubeService)) {
$this->output->writeln("\t Finished creating [".$oauthClient['type']."] oauth youtube service for channel [".$oauthClient['channel_title']."] and client [".$oauthClient['oauth_client_name']."]");
}
return [ 'access_token_id' => $accessTokenId, 'youtube_service' => $youTubeService ];
}
protected function refreshOauthClientAccessToken($oauthClient, \Google_Service_YouTube $youTubeService)
{
if (!isset($oauthClient['refresh_token'])) {
$this->output->writeln("\t No refresh_token with which to refresh [".$oauthClient['type']."] oauth access token having id [".$oauthClient['access_token_id']."]");
return false;
}
$refreshToken = $oauthClient['refresh_token'];
$this->output->writeln("\t Refreshing [".$oauthClient['type']."] oauth access token having id [".$oauthClient['access_token_id']."]");
$googleClient = $youTubeService->getClient();
$refreshResponse = $googleClient->refreshToken($refreshToken);
if (array_key_exists('access_token', $refreshResponse)) {
$googleClient->setAccessToken($refreshResponse);
$this->updateOauthAccessTokenInDB($oauthClient['access_token_id'], $refreshResponse, $youTubeService->getClient()->getScopes(), $this->conn);
$this->output->writeln("\t Refreshed [".$oauthClient['type']."] oauth access token having id [".$oauthClient['access_token_id']."]");
return true;
} else {
$this->output->writeln("\t No access_token returned during refresh of [".$oauthClient['type']."] oauth access token having id [".$oauthClient['access_token_id']."]");
return false;
}
}
protected function obtainAuthorizationInteractively($oauthClient, \Google_Service_YouTube $youTubeService)
{
$googleClient = $youTubeService->getClient();
$googleClient->setPrompt('consent');
$authUrl = $googleClient->createAuthUrl();
if ($this->isCommandLine === false) {
$this->initializeOauthAccessTokenInDB($oauthClient, $googleClient->getRedirectUri(), $googleClient->getScopes());
throw new InteractiveAuthorizationRequiredException($authUrl);
}
$this->output->writeln("\t Auth URL: [$authUrl]");
$this->output->writeln("\t Type 'yes' or 'y' to continue after auth is complete: ");
$handle = fopen("php://stdin","r"); // read from STDIN
$line = trim(fgets($handle));
if ($line !== 'yes' && $line !== 'y') {
$this->output->writeln("Cancelling user authorization...");
return null;
}
$accessTokenId = YouTubeServiceBuilder::completeAuthentication($oauthClient, null, $this->conn, $youTubeService);
return $accessTokenId;
}
protected static function createYouTubeApiKeyService(array $apiKeyInfo): \Google_Service_YouTube
{
$googleClient = new \Google_Client();
$googleClient->setAccessType("offline");
$googleClient->setDeveloperKey($apiKeyInfo['key']);
$youTubeService = new \Google_Service_YouTube($googleClient);
return $youTubeService;
}
/**
* @param array $oauthClient (result of getOauthClientsFromDB or getOauthAccessTokenFromDB)
* @param string $redirectUri
* @return \Google_Service_YouTube
*/
protected static function createYouTubeService($oauthClient, $redirectUri = null)
{
$googleClient = new \Google_Client();
$googleClient->setClientId($oauthClient['client_id']);
$googleClient->setClientSecret($oauthClient['client_secret']);
$googleClient->setAccessType("offline");
$googleClient->setState($oauthClient['user_account_email']."|".$oauthClient['project_name']."|".$oauthClient['channel_title']."|".$oauthClient['oauth_client_name']);
if (!empty($oauthClient['scopes'])) {
$googleClient->setScopes($oauthClient['scopes']);
}
if (!is_null($redirectUri)) {
$googleClient->setRedirectUri($redirectUri);
} else if (isset($oauthClient['redirect_uri'])) {
$googleClient->setRedirectUri($oauthClient['redirect_uri']);
}
$youTubeService = new \Google_Service_YouTube($googleClient);
return $youTubeService;
}
public static function completeAuthentication($oauthClient, $authCode, \Doctrine\DBAL\Connection $conn, \Google_Service_YouTube $youTubeService = null) {
// Assumes it was just updated with a new Auth code
$token = YouTubeServiceBuilder::getOauthAccessTokenFromDB(
$oauthClient['user_account_email'],
$oauthClient['project_name'],
$oauthClient['channel_title'],
$oauthClient['oauth_client_name'],
$conn);
$authCode = $authCode ?? $token['auth_code'];
if (is_null($youTubeService)) {
// We are probably being called by CallbackController. Therefore, "initializeOauthAccessTokenInDB"
// should have been called earlier setting scopes and redirect_uri for our DB access token row:
$youTubeService = YouTubeServiceBuilder::createYouTubeService($token, $token['redirect_uri']);
}
$googleClient = $youTubeService->getClient();
// $output->writeln("\t Fetching [".$token['type']."] oauth access token (having id [".$token['access_token_id']."]) with auth code");
$accessTokenInfo = $googleClient->fetchAccessTokenWithAuthCode($authCode);
$googleClient->setAccessToken($accessTokenInfo);
// TODO: or should the scopes be added by the callback handler?
YouTubeServiceBuilder::updateOauthAccessTokenInDB($token['access_token_id'], $accessTokenInfo, $googleClient->getScopes(), $conn);
return $token['access_token_id'];
}
public function getQuotaExceeded()
{
$today = (new \DateTime())->setTime(0, 0);
if (isset($this->quotaExceeded)) {
$db = clone $this->quotaExceeded;
if ($today->diff($db->setTime(0, 0))->days > 0) {
// If there is a midnight between now and when the quota was exceeded:
$this->resetQuotaExceeded();
}
}
return $this->quotaExceeded;
}
public function getUploadLimitExceeded()
{
$today = (new \DateTime())->setTime(0, 0);
if (isset($this->uploadLimitExceeded)) {
$db = clone $this->uploadLimitExceeded;
if ($today->diff($db->setTime(0, 0))->days > 0) {
// If there is a midnight between now and when the limit was exceeded:
$this->resetUploadLimitExceeded();
}
}
return $this->uploadLimitExceeded;
}
public function resetQuotaExceeded()
{
$this->quotaExceeded = $this->setLimitExceededAt('quota_exceeded_at', $this->quotaExceeded, true);
}
public function notifyQuotaExceeded()
{
$this->quotaExceeded = $this->setLimitExceededAt('quota_exceeded_at', $this->quotaExceeded, false);
}
public function resetUploadLimitExceeded()
{
$this->uploadLimitExceeded = $this->setLimitExceededAt('upload_limit_exceeded_at', $this->uploadLimitExceeded, true);
}
public function notifyUploadLimitExceeded()
{
$this->uploadLimitExceeded = $this->setLimitExceededAt('upload_limit_exceeded_at', $this->uploadLimitExceeded, false);
}
protected function setLimitExceededAt($column, $currValue, $reset = false)
{
$nullCheck = (false === $reset) ? "AND gap.$column IS NULL" : "";
$atValue = (false === $reset) ? new \DateTime() : null;
$query =
"UPDATE google_api_project gap
SET
gap.$column = :atValue
WHERE gap.id = :projectRowId
$nullCheck";
$params = array(
"projectRowId" => $this->projectRowId,
"atValue" => $atValue
);
$types = array (
"atValue" => "datetime"
);
DatabaseUtils::makeStatementQuery($this->conn, $query, $params, $types);
if ($currValue instanceof \DateTime && $atValue instanceof \DateTime) {
return $currValue;
} else {
return $atValue;
}
}
protected function pullLimitsExceededAt()
{
$query =
"SELECT quota_exceeded_at, upload_limit_exceeded_at
FROM google_api_project gap
WHERE gap.id = :projectRowId";
$params = array(
"projectRowId" => $this->projectRowId
);
$results = DatabaseUtils::makeSelectQueryFetchAll($this->conn, $query, $params);
if (count($results) == 1) {
$this->quotaExceeded = null;
$this->uploadLimitExceeded = null;
if (isset($results[0]['quota_exceeded_at'])) {
$this->quotaExceeded = \DateTime::createFromFormat("Y-m-d H:i:s", $results[0]['quota_exceeded_at']);
}
if (isset($results[0]['upload_limit_exceeded_at'])) {
$this->uploadLimitExceeded = \DateTime::createFromFormat("Y-m-d H:i:s", $results[0]['upload_limit_exceeded_at']);
}
} else {
throw new \Exception("Unexpected [".count($results)."] number of Google YouTube account records match user account [$this->userAccount] and channel title [$this->channelTitle]");
}
}
public function getGoogleApiProjectFromDB()
{
$query =
"SELECT gap.*
FROM google_api_project gap
INNER JOIN google_user_account gua on gua.id = gap.google_user_account_id
WHERE gap.project_name = :projectName
AND gua.user_account_email = :userAccountEmail";
$params = array(
"userAccountEmail" => $this->userAccountEmail,
"projectName" => $this->projectName
);
$results = DatabaseUtils::makeSelectQueryFetchAll($this->conn, $query, $params);
if (count($results) == 1) {
return $results[0];
}
if (count($results) == 0) {
return null;
}
throw new \Exception("More than one Google api project record matches project name [$this->projectName] and user account [$this->userAccountEmail]");
}
public function getUserChannelIdFromDB()
{
$query =
"SELECT gya.id, user_id, channel_id
FROM google_youtube_account gya
INNER JOIN google_user_account gua on gua.id = gya.google_user_account_id
INNER JOIN google_api_project gap on gua.id = gap.google_user_account_id
WHERE gap.id = :projectRowId
AND gya.channel_title = :channelTitle";
$params = array(
"projectRowId" => $this->projectRowId,
"channelTitle" => $this->channelTitle
);
$results = DatabaseUtils::makeSelectQueryFetchAll($this->conn, $query, $params);
if (count($results) == 1) {
return [ 'id' => $results[0]['id'], 'userId' => $results[0]['user_id'], 'channelId' => $results[0]['channel_id'] ];
}
if (count($results) == 0) {
return null;
}
throw new \Exception("More than one Google YouTube account record matches project row ID [$this->projectRowId] and channel title [$this->channelTitle]");
}
protected function getOauthClientNameFromDB($clientType)
{
$query =
"SELECT goc.name as client_name
FROM google_api_oauth_client goc
INNER JOIN google_api_project gap on gap.id = goc.google_api_project_id
WHERE gap.id = :projectRowId
AND goc.type = :clientType";
$params = array(
"projectRowId" => $this->projectRowId,
"clientType" => $clientType
);
$results = DatabaseUtils::makeSelectQueryFetchAll($this->conn, $query, $params);
if (count($results) == 1) {
return $results[0]['client_name'];
}
if (count($results) == 0) {
return null;
}
throw new \Exception("More than one Google oauth client record matches project row ID [$this->projectRowId] and client type [$clientType]");
}
protected function initializeOauthAccessTokenInDB($oauthClient, $redirectUri, array $scopes = [])
{
$oauthAccessTokenId = isset($oauthClient['access_token_id']) ? $oauthClient['access_token_id'] : null;
$params = array();
if (is_null($oauthAccessTokenId)) {
// Insert query
$query = "
INSERT INTO google_api_oauth_access_token
(google_api_oauth_client_id, google_youtube_account_id, scopes, redirect_uri)
SELECT
(:oauthClientRowId),
(:youTubeAccoutRowId),
(:scopes),
(:redirectUri)";
$params["oauthClientRowId"] = $oauthClient['client_row_id'];
$params["youTubeAccoutRowId"] = $oauthClient['account_row_id'];
} else {
// Update query
$query = "
UPDATE google_api_oauth_access_token
SET
scopes = :scopes,
redirect_uri = :redirectUri
WHERE id = :id";
$params["id"] = $oauthAccessTokenId;
}
$params["redirectUri"] = $redirectUri;
if (!empty($scopes)) {
$params["scopes"] = implode(' ', $scopes);
} else {
$params["scopes"] = null;
}
DatabaseUtils::makeStatementQuery($this->conn, $query, $params);
if (is_null($oauthAccessTokenId)) {
$oauthAccessTokenId = $this->conn->lastInsertId();
}
return $oauthAccessTokenId;
}
protected static function updateOauthAccessTokenInDB($id, $accessTokenInfo, array $scopes = [], \Doctrine\DBAL\Connection $conn)
{
// setting auth code to NULL since it has already been redeemed
$query =
"UPDATE google_api_oauth_access_token t
SET
access_token = :accessToken,
token_type = :tokenType,
expires_in = :expiresIn,
created = :created,
refresh_token = :refreshToken,
auth_code = NULL,
scopes = :scopes
WHERE id = :id";
$params = array(
"id" => $id,
"accessToken" => isset($accessTokenInfo['access_token']) ? $accessTokenInfo['access_token'] : null,
"tokenType" => isset($accessTokenInfo['token_type']) ? $accessTokenInfo['token_type'] : null,
"expiresIn" => isset($accessTokenInfo['expires_in']) ? $accessTokenInfo['expires_in'] : null,
);
$types = array();
if (isset($accessTokenInfo['created'])) {
$params["created"] = \DateTime::createFromFormat('U', $accessTokenInfo['created']);
$types["created"] = "datetime";
} else {
$params["created"] = null;
}
// Only shows up if prompt='consent' when doing auth?
if (array_key_exists('refresh_token', $accessTokenInfo)) {
$params["refreshToken"] = $accessTokenInfo['refresh_token'];
} else {
$params["refreshToken"] = null;
}
if (!empty($scopes)) {
$params["scopes"] = implode(' ', $scopes);
} else {