Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3831

Youtube api with auth 2 in cron jobs

$
0
0

I have created a php script for fetching youtube videos data of my youtube channel including unlisted videos with the help of auth2.0 . My problem is how I run script in cron job without any user interaction?Cant use service account as youtube api doesnt support it and how script chooses which account to choose ?

<?phprequire_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));}?>

And the oauth2callback.php code is

<?phprequire_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));}?>

I want to run it without any user interaction in cron job without any browser. Cant figure out how.


Viewing all articles
Browse latest Browse all 3831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>