YouTube数据API v3 nextPageToken太长,以至于我尝试Limit Exceeded: URLFetch URL Length
时Google Apps脚本返回错误UrlFetchApp.fetch("requesturl")
;
这是请求网址
www.googleapis.com/youtube/v3/commentThreads?key="+API_KEY+"&part=id&videoId="+ VIDEO_ID +"&order=relevance&textFormat="plaintext&pageToken=" + nextPageToken
代码
var API_KEY = '***************************************';
var VIDEO_ID = 'oG2Ka3nOVzM';
var maxResults = 100 ;
var pageToken = "";
var order = "relevance";
var textFormat = "plaintext";
url_commentthreads_1 = "https://www.googleapis.com/youtube/v3/commentThreads?key="+API_KEY+"&part=id&maxResults=100&videoId="+ VIDEO_ID +"&order="+ order +"&textFormat=" + textFormat + "&pageToken="
var response_commentthreads_1 = UrlFetchApp.fetch(url_commentthreads_1);
if (response_commentthreads_1.getResponseCode() != 200) {
Logger.log("Error %s:", response_commentthreads_1);
return;
}
var json_commentthreads_1 = JSON.parse(response_commentthreads_1.getContentText());
var items = json_commentthreads_1['items'];
var nextPageToken = json_commentthreads_1['nextPageToken'];
Logger.log(nextPageToken);
var length_1 = items.length;
var array_1 = [];
for (var i = 0; i < length_1; i++) { //コメントIDの数だけIDの配列を作る
array_1.push(items[i]['id'])
}
Logger.log(array_1);
while(nextPageToken != undefined){
url_commentthreads = "https://www.googleapis.com/youtube/v3/commentThreads?key="+API_KEY+"&part=id&videoId="+ VIDEO_ID +"&order="+ order +"&textFormat=" + textFormat + "&pageToken=" + nextPageToken
Logger.log(url_commentthreads);
var response_commentthreads = UrlFetchApp.fetch(url_commentthreads); //This returns the error "Limit Exceeded: URLFetch URL Length."
var json_commentthreads = JSON.parse(response_commentthreads.getContentText());
var items_new = json_commentthreads['items'];
var length_new = items_new.length;
Logger.log(length_new);
for (var i = 0; i < length_new; i++) { //コメントIDの数だけIDの配列を作る
array_1.push(items_new[i]['id'])
}
var nextPageToken = json_commentthreads['nextPageToken'];
Logger.log(nextPageToken);
}
有人可以帮助避免此错误吗?
nextpage令牌之一就是这样。
pageToken=QURTSl9pM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2I4WHNnX0ZYeEF1RmFtcUk0TFFHZnhPbzBEa0dJWTMtWVYwRVZtRVFtdEl2b0RkSjZxSjVwdERBUjBxOTlEUXZGM1ZQbThua0tkdjk2U0MxRkFpcm5VYnR5NVI1dTNfS2N3Y1VSYm13b2I2XzdJU1hnLTM0NzRWZTVMeFJRWk4wMVJqbjF3QVV5M3J4WUd6a0hkM0QxYV9faVlfMUc4Sk5JMjJ0cXRYblRKbG50Ui0wcGwzSm1JQV9iUWpRemVaY0Rta1Z2WnVGYWtJS3plSXBfT3pVZ2p0cWJ0WEpzeVJqWVJIWkxRMV9uTVhqdTNBazJ6eTAtY3V5d2kzeTBnYUR0V3ZJVzFHYUZsSXBrcXVxX2dSM1N2TFB4U0lkNFl1Z2RhOTZLTHBqcld0ZHRzYWhDcVVzN0VkQ01fVGZSM0hNN085YktXelpKLUNmOG9jZ3NuTjRCdmVuQ3JCQjVBRVBmeFZKa0JJZVotaDVtQ1Vrb1gtNm9mMzJHWkt4ODJBcWpCd3RzamdJNzl4QkM5VzlCUUZNNjhmemtfeFhfYzQ5UG5uN1lCZjM0cmZXXzJtQmZhczVxSlVLaTdsc05fUkNQUGd0S1gyNm02UGV6NXcwUkc4dFBzVGdQbHRmRm5ic0t2bk5Rd0pDakVqb2c4bEdxUk9Db3RHUkpMT1picFN1blpjUTBfN1JXQ04ySTRhcTZIQmUwOG9PNW1DeGQ3WXRibnd3N2ZlaTZyYm9hcGpoUDM1YnkyelZUTnZMaG5FbDlfQlNSSGpXaGFtMm5uaXVEQmdHLVJYQTdZT2pLNUVuVHluX0NpRk1mV2FSQVBSZHAtWTFkSWVOZkl5eUhDU21kNE81QklNZTNLM3lSUGVoc0dYWA==
答案 0 :(得分:3)
这个答案怎么样?请认为这只是几个可能的答案之一。
从包含查询参数的端点获取以下结果。
"https://www.googleapis.com/youtube/v3/commentThreads?key="+API_KEY+"&part=id&maxResults=100&videoId="+ VIDEO_ID +"&order="+ order +"&textFormat=" + textFormat + "&pageToken="
nextPageToken
的值长度为1832。"https://www.googleapis.com/youtube/v3/commentThreads?part=id&maxResults=100&videoId="+ VIDEO_ID +"&order="+ order
textFormat
。nextPageToken
的值长度为1832。"https://www.googleapis.com/youtube/v3/commentThreads?part=id&maxResults=100&videoId="+ VIDEO_ID
textFormat
和order=relevance
的参数。nextPageToken
的值长度为176。order=time
时,nextPageToken
的值长度为172。"https://www.googleapis.com/youtube/v3/commentThreads?part=id&maxResults=10&videoId="+ VIDEO_ID
textFormat
和order
的参数。并将maxResults
减少到10。nextPageToken
的值长度为172。从以上结果中发现,使用order=relevance
时,nextPageToken
的值长度变长。
在您的脚本中,URL的长度似乎超过2 KB。我认为这样,您的脚本中就会发生错误。
为了避免这种情况,以下模式如何?
删除查询参数order=relevance
。
减少maxResults
。
maxResults
的默认端点为10、25和50时,nextPageToken
的值长度分别为328、580和1000。使用高级Google服务而不是UrlFetchApp。
在使用此脚本之前,please enable YouTube Data API at Advanced Google Service.
// var API_KEY = '***************************************';
var VIDEO_ID = 'oG2Ka3nOVzM';
var maxResults = 100 ;
var pageToken = "";
var order = "relevance";
var textFormat = "plaintext";
// I added below script.
var json_commentthreads_1 = YouTube.CommentThreads.list("id", {maxResults: maxResults, videoId: VIDEO_ID, order: order, textFormat: textFormat});
var items = json_commentthreads_1['items'];
var nextPageToken = json_commentthreads_1['nextPageToken'];
Logger.log(nextPageToken);
var length_1 = items.length;
var array_1 = [];
for (var i = 0; i < length_1; i++) { //Create an array of IDs for each comment ID
array_1.push(items[i]['id'])
}
Logger.log(array_1);
while(nextPageToken != undefined){
// I added below script.
var json_commentthreads = YouTube.CommentThreads.list("id", {maxResults: maxResults, videoId: VIDEO_ID, order: order, textFormat: textFormat, pageToken: nextPageToken});
var items_new = json_commentthreads['items'];
var length_new = items_new.length;
Logger.log(length_new);
for (var i = 0; i < length_new; i++) { //Create an array of IDs for each comment ID
array_1.push(items_new[i]['id'])
}
var nextPageToken = json_commentthreads['nextPageToken'];
Logger.log(nextPageToken);
}
在这种模式下,stvar's answer被反映到OP的脚本中。 GET请求将转换为POST请求。在这种情况下,无法使用API密钥。因此使用了访问令牌。
// var API_KEY = '***************************************';
var VIDEO_ID = 'oG2Ka3nOVzM';
var maxResults = 100 ;
var pageToken = "";
var order = "relevance";
var textFormat = "plaintext";
url_commentthreads_1 = "https://www.googleapis.com/youtube/v3/commentThreads?part=id&maxResults=100&videoId="+ VIDEO_ID +"&order="+ order +"&textFormat=" + textFormat + "&pageToken="
var response_commentthreads_1 = UrlFetchApp.fetch(url_commentthreads_1, {method: "post", headers: {"x-http-method-override": "GET", Authorization: "Bearer " + ScriptApp.getOAuthToken()}});
if (response_commentthreads_1.getResponseCode() != 200) {
Logger.log("Error %s:", response_commentthreads_1);
return;
}
var json_commentthreads_1 = JSON.parse(response_commentthreads_1.getContentText());
var items = json_commentthreads_1['items'];
var nextPageToken = json_commentthreads_1['nextPageToken'];
Logger.log(nextPageToken);
var length_1 = items.length;
var array_1 = [];
for (var i = 0; i < length_1; i++) {
array_1.push(items[i]['id'])
}
Logger.log(array_1);
while(nextPageToken != undefined){
url_commentthreads = "https://www.googleapis.com/youtube/v3/commentThreads?part=id&videoId="+ VIDEO_ID +"&order="+ order +"&textFormat=" + textFormat + "&pageToken=" + nextPageToken
Logger.log(url_commentthreads);
var response_commentthreads = UrlFetchApp.fetch(url_commentthreads, {method: "post", headers: {"x-http-method-override": "GET", Authorization: "Bearer " + ScriptApp.getOAuthToken()}}); //This returns the error "Limit Exceeded: URLFetch URL Length."
var json_commentthreads = JSON.parse(response_commentthreads.getContentText());
var items_new = json_commentthreads['items'];
var length_new = items_new.length;
Logger.log(length_new);
for (var i = 0; i < length_new; i++) {
array_1.push(items_new[i]['id'])
}
var nextPageToken = json_commentthreads['nextPageToken'];
Logger.log(nextPageToken);
}
答案 1 :(得分:3)
您的问题源自已知的API问题,该问题早在here的Stackoverflow上已经发现。
我将总结发现的东西-Google员工发现了confirmed。您可以阅读上面引用的SO线程的所有详细信息。
从设计上限制从Youtube的CommentThreads.list端点获取结果集,因为API中断,因此pageToken
太长(完全没有记录长度限制)时会返回错误。
如果pageToken
的长度足以使要传递给API的URL的长度超过2K,则应该用等效的GET
替换POST
方法。为了说服自己,请看一下Google自己的公共Python code。对于Google发布的任何其他客户端库,其工作方式都相似。