问题描述
YouTube数据API v3 nextPageToken太长,导致我尝试UrlFetchApp.fetch("requesturl")
时Google Apps脚本返回错误Limit Exceeded: URLFetch URL Length
;
YouTube data api v3 nextPageToken is too long that Google Apps Script returns an error Limit Exceeded: URLFetch URL Length
when I tried 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);
}
有人可以帮助避免此错误吗?
Could anyone help to avoid this error?
nextpage令牌之一就是这样.pageToken=QURTSl9pMFo0dE9oaEE0T2pHWEJYNGk2dkx4ZzZkYW5vc2xET0QyWk1wUUEwVGZ3YmF5NE4wUENzT2V5dFdwRDZJaVBJdFpHU3ZqRXhFcEFDWnd6YWJ3VmhDSkJVb3BqUEVxakVxeE5XZ2JmeGh6cnhfN2k1VmNZd3VGRWRFS1hvRG4zemZCeEl2ajU1RGVrYzE1LVNBZlJJMVUwUTFMMGV4VTE2X25VSDMwMnlZY1gwdFFXUl9DYnpuTUZwZnAyWGVnZ0lwNU9sYzNnOG5aQU1QRDRGdGhFbDdJZ2pVT0J5Tzg2dVVKbWw2NF93NTFuaUNmNThwT3o3blNNMkQwTlluTFBsWjNxamlpbk8wMnhiYXVuY28za282NWxPVHJYR3ZrLWRZWVVjWTduQ1c2NmEyQmtmS2Y0Z1g5NzJyRXJCM0VnN3pDdXRtaWt5X245Vm52Uy1oS0pSM2R5RHVqSlYxQXhkY3pLRE1idUdKN2wwaDFSVE9wNzNVek5wbG9DekhfbkRhOEVlSjRaSmJabUs1YVlvZ1VDbEhwVkUxdVMzOHJIQWxDcWtSc182MTBKUTBMdkNHZ0R3ZXFXamdyZGZIemZpMnlJRnBMQV84OXBsRFN0MGpvcG4td2xnZ1BpR3g2Q1hPZERnTTluMDlEN09IWTNvRDJ1TXMzbDQ1ZWlhQU5NalhrQUg2dnNhb21ibVZWbk9LX1JsZzF2dG1TVzZvbHZ0SDNNN3ZqSDVjZGJ2SGplQnpmbzBxU3h5bDc5eks5Y2ljWjBuaFZuUW5VWHdJVFJZTHJUVTZKbnJtc3R4ZkY2Zkh2Q0Y5Uzk5Y0NaU29EQVBQcWZfRDgweDdETEtWOEx6S2kxb1ZyYlNJOXVaYVlvQmxQT21HLUhWMm5rNWwzYjB3VFZzcXR4RmlkanlHbFlvU1NsUThBaVllYy1qNHRkbVlMNFJz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BEa0dJWTMtWVYwRVZtRVFtdEl2b0RkSjZxSjVwdERBUjBxOTlEUXZGM1ZQbThua0tkdjk2U0MxRkFpcm5VYnR5NVI1dTNfS2N3Y1VSYm13b2I2XzdJU1hnLTM0NzRWZTVMeFJRWk4wMVJqbjF3QVV5M3J4WUd6a0hkM0QxYV9faVlfMUc4Sk5JMjJ0cXRYblRKbG50Ui0wcGwzSm1JQV9iUWpRemVaY0Rta1Z2WnVGYWtJS3plSXBfT3pVZ2p0cWJ0WEpzeVJqWVJIWkxRMV9uTVhqdTNBazJ6eTAtY3V5d2kzeTBnYUR0V3ZJVzFHYUZsSXBrcXVxX2dSM1N2TFB4U0lkNFl1Z2RhOTZLTHBqcld0ZHRzYWhDcVVzN0VkQ01fVGZSM0hNN085YktXelpKLUNmOG9jZ3NuTjRCdmVuQ3JCQjVBRVBmeFZKa0JJZVotaDVtQ1Vrb1gtNm9mMzJHWkt4ODJBcWpCd3RzamdJNzl4QkM5VzlCUUZNNjhmemtfeFhfYzQ5UG5uN1lCZjM0cmZXXzJtQmZhczVxSlVLaTdsc05fUkNQUGd0S1gyNm02UGV6NXcwUkc4dFBzVGdQbHRmRm5ic0t2bk5Rd0pDakVqb2c4bEdxUk9Db3RHUkpMT1picFN1blpjUTBfN1JXQ04ySTRhcTZIQmUwOG9PNW1DeGQ3WXRibnd3N2ZlaTZyYm9hcGpoUDM1YnkyelZUTnZMaG5FbDlfQlNSSGpXaGFtMm5uaXVEQmdHLVJYQTdZT2pLNUVuVHluX0NpRk1mV2FSQVBSZHAtWTFkSWVOZkl5eUhDU21kNE81QklNZTNLM3lSUGVoc0dYWA==
推荐答案
这个答案怎么样?请认为这只是几个可能的答案之一.
How about this answer? Please think of this as just one of several possible answers.
从您的端点(包括查询参数)获得以下结果.
From your endpoint including the query parameters, the following results are obtained.
-
"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?key="+API_KEY+"&part=id&maxResults=100&videoId="+ VIDEO_ID +"&order="+ order +"&textFormat=" + textFormat + "&pageToken="
- This is the endpoint you are using.
- When above endpoint is used, the value length of
nextPageToken
is 1832.
- 删除了
textFormat
的参数. - 使用上方端点时,
nextPageToken
的值长度为1832.
- Removed a parameter of
textFormat
. - When above endpoint is used, the value length of
nextPageToken
is 1832.
- 删除了
textFormat
和order=relevance
的参数. - 使用上方端点时,
nextPageToken
的值长度为176. - 使用
order=time
时,nextPageToken
的值长度为172.
- Removed a parameter of
textFormat
andorder=relevance
. - When above endpoint is used, the value length of
nextPageToken
is 176. - When
order=time
is used, the value length ofnextPageToken
was 172.
- 删除了
textFormat
和order
的参数.并将maxResults
减少到10. - 使用上方端点时,
nextPageToken
的值长度为172.
- Removed a parameter of
textFormat
andorder
. And reducedmaxResults
to 10. - When above endpoint is used, the value length of
nextPageToken
is 172.
从以上结果中发现,使用order=relevance
时,nextPageToken
的值长度变长.
From above results, it was found that when order=relevance
is used, the value length of nextPageToken
becomes long.
在您的脚本中,URL的长度似乎超过了2 KB.我认为这样,您的脚本中就会发生错误.
And in your script, it seems that the length of URL is more than 2 kbytes. I think that by this, an error occurs in your script.
为了避免这种情况,以下模式如何?
In order to avoid this, how about the following patterns?
删除order=relevance
的查询参数.
减少maxResults
.
- 当默认端点的
maxResults
分别为10、25和50时,nextPageToken
的值长度分别为328、580和1000.
- When
maxResults
is 10, 25 and 50 for your default endpoint, the value length ofnextPageToken
is 328, 580 and 1000, respectively.
使用高级Google服务而不是UrlFetchApp.
Use Advanced Google Service instead of UrlFetchApp.
- 使用高级Google服务的YouTube数据API时,我可以确认不会发生此类错误.不幸的是,我不确定Advanced Google Service的YouTube Data API的内部流程.因此我无法获得未发生错误的原因.我为此表示歉意.
在使用此脚本之前,请在高级处启用YouTube数据API Google服务.
// 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);
}
参考文献:
- CommentThreads:列表
- 高级Google服务
- CommentThreads: list
- Advanced Google Service
References:
在这种模式下, stvar的答案反映在OP的脚本中. GET请求将转换为POST请求.在这种情况下,无法使用API密钥.因此使用了访问令牌.
In this pattern, stvar's answer was reflected to OP's script. The GET request is converted to the POST request. In this case, API key cannot be used. So the access token is used.
// 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);
}
这篇关于Google Apps脚本返回错误“超出限制:URLFetch URL长度";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!