gpt4 book ai didi

javascript - 使用google api或任何其他方法获取特定范围内的youtube channel 视频

转载 作者:行者123 更新时间:2023-12-03 06:08:11 26 4
gpt4 key购买 nike

我尝试使用Google API从播放列表中获取YouTube视频。成功了。但是我最多可以得到50个视频。我需要做的是,获取50个以上的视频,或获取该范围内最古老的视频。

就像我们可以限制mysql的结果一样。

语言(PHP和Java脚本)

我的工作代码

<script src="https://apis.google.com/js/api.js"></script>
<script>
/**
* Sample JavaScript code for youtube.playlistItems.list
* See instructions for running APIs Explorer code samples locally:
* https://developers.google.com/explorer-help/guides/code_samples#javascript
*/

function loadClient() {
gapi.client.setApiKey("MY_API_KEY");
return gapi.client.load("https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest")
.then(function() { console.log("GAPI client loaded for API"); },
function(err) { console.error("Error loading GAPI client for API", err); });
}
// Make sure the client is loaded before calling this method.
function execute() {
return gapi.client.youtube.playlistItems.list({
"part": "snippet,contentDetails",
"maxResults": 50,
"playlistId": "PLAY_LIST_ID"
})
.then(function(response) {
// Handle the results here (response.result has the parsed body).
console.log("Response", response);
},
function(err) { console.error("Execute error", err); });
}
gapi.load("client");
</script>


我阅读了Google api文档,但没有明确的答案。
请不要关闭问题。我需要明确的答案。

最佳答案


<?php

class GAPI{

private $appName = "Get Play List Info";
private $apiKey = 'YOUR_API_KEY';
private $maxResult = NULL; //0-50
private $playlistId = NULL;
private $nextPageToken = NULL;
private $prevPageToken = NULL;

private $service;
private $client;

public function __construct($playlistId,$maxResult=10){

$this->init(); //Initiate function
$this->client = new Google_Client(); //Create object from Google Client class

$this->playlistId = $playlistId; //Set play list id
$this->maxResult = $maxResult; //Set max results

$this->client->setApplicationName($this->appName);
$this->client->setDeveloperKey($this->apiKey);

// Define service object for making API requests.
$this->service = new Google_Service_YouTube($this->client);
}

//Initiate
private function init(){
if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
throw new Exception(sprintf('Please run "composer require google/apiclient:~2.0" in "%s"', __DIR__));
}
require_once __DIR__ . '/vendor/autoload.php';
}

public function getItems($nextPageToken=NULL,$prevPageToken=NULL){

$this->nextPageToken = isset($nextPageToken) ? $nextPageToken : NULL;
$this->prevPageToken = isset($prevPageToken) ? $prevPageToken : NULL;

$queryParams = [
'maxResults' => $this->maxResult,
'playlistId' => $this->playlistId,
'pageToken' => $this->nextPageToken
];

$response = $this->service->playlistItems->listPlaylistItems('contentDetails', $queryParams);

return $response;
}

}

?>

关于javascript - 使用google api或任何其他方法获取特定范围内的youtube channel 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61045454/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com