问题
I'm working on a simple program, and this is my first time using the YT API. I want to get all comments from any video, I can't do it without "pageToken," someone told me, but I can't seem to get what it does or how to use it. I need help. I'm using node.js.
回答1:
Follow this document: https://developers.google.com/youtube/v3/quickstart/nodejs It will answer all your questions.
回答2:
Top-Level Comments and Associated Replies
According to the official docs, there are two API endpoints at one's disposal w.r.t. reading the comments attached to any given video -- identified by its ID:
- CommentThreads.list and
- Comments.list.
The comments of any given video are structured such that the first endpoint above returns a paginated set of top-level comments (i.e. CommentThreads
resource) when invoked with parameter videoId set as videoId=VIDEO_ID
, where VIDEO_ID
is the ID of the video of your interest.
The second API endpoint above is to be used for obtaining the paginated set of all comment replies (i.e. Comments
resource) attached to any given top-level comment, by specifying to its parameter parentId the ID of the respective top-level comment.
Note that, even if the CommentThreads
resource contains a list of comment replies within its property replies, according to the docs, that list is incomplete. That is the reason why one need using the CommentThreads.list
endpoint in tandem with Comments.list
endpoint.
The property nextPageToken
and the parameter pageToken
Now, as mentioned above, the API returns sets of CommentThreads
and, respectively, Comments
resources. These sets are paginated, since each API call will return (by design) no more that 50 such items.
Consequently, for one to fetch all CommentThreads
resources of a given video through CommentThreads.list
API endpoint, will have to implement pagination. The same is true w.r.t. the Comments.list
API endpoint when it comes to fetch all Comments
resources of any given top-level comment.
The meaning of pagination is simply the following: for to obtain the n
-th page of a paginated result set, where n >= 2
, one has to extract the value of the property nextPageToken
from the n-1
-th page as, say, PAGE_TOKEN
, and pass on that value to the n
-th API invoking URL to the parameter pageToken
as pageToken=PAGE_TOKEN
. If a given page does not contain the property nextPageToken
, then the pagination reached its end.
API Limitations Imposed by Design
According to official Google staff statements, the number of items obtained by paginating the result sets of CommentThreads.list
endpoint is limited.
That limit is not specified, thus one needs to take into account, that, in cases, it will not be possible to obtain all top-level comments of a given video. This is unfortunate, but a fact.
来源:https://stackoverflow.com/questions/64029492/how-do-i-use-pagetoken