我创建了一个PHP脚本来获取我的YouTube频道的YouTube视频数据,包括未列出的视频与auth2.0的帮助。我的问题是我如何运行脚本在cron作业没有任何用户交互?不能使用服务帐户作为youtube API不支持它,脚本如何选择哪个帐户选择?
<?php
require_once __DIR__ . '/vendor/autoload.php';
session_start();
if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
throw new Exception(sprintf('Please run "composer require google/apiclient:~2.0" in "%s"', __DIR__));
}
$client = new Google_Client();
$client->setApplicationName('youtubeproject');
$client->setDeveloperKey(" ");
$client->setAccessType('offline');
$client->setScopes([
'https://www.googleapis.com/auth/youtube.readonly',
]);
$client->setAuthConfig(' ');
//$client->setApprovalPrompt('consent');
//$client->setIncludeGrantedScopes(true);
$client->setPrompt('consent');
$client->setIncludeGrantedScopes(true);
$client->createAuthUrl();
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
$youtube = new Google_Service_YouTube($client);
$i=0;
$pagetoken='';
$pagetoken1='';
youtube_calls_playlist($pagetoken);
youtube_callsplaylistitems($pagetoken1);
} else {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
$redirect_uri = 'http://localhost/youtubeproject/' . 'oauth2callback.php/';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
function youtube_calls_playlist($pagetoken){
global $youtube;
global $total_ids;
if($pagetoken!=''){
$queryParams_playlists = [
'mine' => true,
'pageToken'=> $pagetoken
];
}
else{
$queryParams_playlists = [
'mine' => true,
];
}
$response_playlists= $youtube->playlists->listPlaylists('snippet', $queryParams_playlists);
foreach ($response_playlists->items as $playlistsid) {
$id= $playlistsid->id;
$ids[] = $id;
}
if(isset($response_playlists->nextPageToken)){
$pagetoken=$response_playlists->nextPageToken;
youtube_calls($pagetoken);
}
$queryParams_uploads = [
'mine' => true
];
$response_uploads = $youtube->channels->listChannels('contentDetails', $queryParams_uploads);
foreach ($response_uploads->items as $uploadsid) {
$upid= $uploadsid->contentDetails->relatedPlaylists->uploads;
$ids1[] = $upid;
}
$total_ids=array_merge($ids,$ids1);
}
function youtube_callsplaylistitems($pagetoken1){
global $youtube;
global $total_ids;
global $result;
if($pagetoken1!=''){
$queryParams = [
'playlistId' => $total_ids,
'pageToken'=> $pagetoken
];
}
else{
$queryParams = [
'playlistId' => $total_ids];
}
$response = $youtube->playlistItems->listPlaylistItems('snippet', $queryParams);
foreach($response->items as $videos){
$videoid=$videos->snippet->resourceId->videoId;
$publishedat=$videos->snippet->publishedAt;
$queryParams_videos= [
'id' => $videoid];
$response_videos = $youtube->videos->listVideos('snippet', $queryParams_videos);
foreach($response_videos->items as $vids){
$i=0;
$description= $vids->snippet->description;
$title= $vids->snippet->title;
$thumbnail=$vids->snippet->thumbnails->default->url;
$videoids[]= $videoid;
$descriptions[]= $description;
$titles[]=$title;
$thumbnails[]=$thumbnail;
}
if(isset($response->nextPageToken)){
$pagetoken1=$response->nextPageToken;
youtube_calls_playlist($pagetoken1);
}
}
$result = array();
$values = array($videoids, $descriptions, $titles, $thumbnails);
foreach($videoids as $index => $key) {
$t = array();
foreach($values as $value) {
$t[] = $value[$index];
}
$result[] = $t;
}
print_r(json_encode($result));
}
?>
php的代码是
<?php
require_once __DIR__.'/vendor/autoload.php';
session_start();
//$client = new Google\Client();
$client = new Google_Client();
$client->setAccessType('offline');
$client->setAuthConfigFile('client_secret_843932249311-9ittju7rjic43jh86g8talo2it8socj2.apps.googleusercontent.com.json');
$client->setRedirectUri('http://localhost/youtubeproject/' . 'oauth2callback.php/');
$client->setScopes([
'https://www.googleapis.com/auth/youtube.readonly',
]);
if (! isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect_uri = 'http://localhost/youtubeproject/index.php' . '/';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
?>
我想运行它没有任何用户交互在cron作业没有任何浏览器。无法弄清楚如何。
2条答案
按热度按时间bihw5rsg1#
在cron作业中运行脚本而无需任何用户交互的最佳方法是使用服务帐户。服务帐户是属于应用程序或服务而非单个用户的帐户。使用服务帐户,您可以在无需任何用户交互的情况下向YouTube API验证您的应用程序。
要创建服务帐户,请按照Google Cloud Platform文档中的说明操作。创建服务帐户后,您需要使用Google API控制台为帐户生成私钥。您将使用私钥向YouTube API验证您的应用程序。
一旦你有了私钥,你就可以使用它来验证你的应用程序到YouTube API。你可以使用Google_Client类来设置验证。下面是一个如何做的例子:
一旦您向YouTube API验证了您的应用程序,您就可以使用该API发出API请求,而无需任何用户交互。然后,您可以将脚本作为cron作业运行,而无需任何用户交互。
vcirk6k62#
我通过获取访问令牌和刷新令牌来更新代码,并将其保存到文件中,然后在cronjob中使用它,而无需进入浏览器和用户交互。