Remove a comment using the youtube data api v3? - youtube-api

As a channel owner I am trying to remove comments posted on my videos and in my discussion section by other users.
I can't find any information on the function in the api documentation. I can call the delete function with a comment id, but it only works on comments that the user who has signed into the api has posted. I'd like to call the same function outlined in this picture and that is available on youtube:
I need to call this remove function from the API
Here's the link to the comment api.

The response I got from Google:
"You cannot delete comments that have not been made by your own account. To remove and moderate comments from other users on videos that you own please use the setModerationStatus API documented here: https://developers.google.com/youtube/v3/docs/comments/setModerationStatus"
You can set the moderation status to rejected and it will have the same functionality.

You will make use of Comments.delete This will require you to pass a commentId to delete a particular comment. There are various ways to fetch the commentId, as for me, I use this URI GET request:
https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&videoId={VIDEO_ID}&key={API_KEY}
You will get a response like:
"items": [
{
"kind": "youtube#commentThread",
"etag": "\"5C5HHOaBSHC5ZXfkrT4ZlRCi01A/wQo7nKuPMjPTCeeV9ofDLff8KhY\"",
"id": "z12ojxq5qwjlulz4o04cc5vxauuaubkbovk0k",
"snippet": {
"videoId": "Ez4nKZ2BeTU",
"topLevelComment": {
"kind": "youtube#comment",
"etag": "\"5C5HHOaBSHC5ZXfkrT4ZlRCi01A/Ifn8tdeFUJiclXThCp44Cib6-cU\"",
"id": "z12ojxq5qwjlulz4o04cc5vxauuaubkbovk0k", //this is the commentId you will use to delete the comment
"snippet": {
"authorDisplayName": "...",
"authorProfileImageUrl": "...",
"authorChannelUrl": "...",
"authorChannelId": {
"value": "..."
},
"videoId": "Ez4nKZ2BeTU",
"textDisplay": "deleteme\ufeff", //this is my comment which I would like to delete
...
Use the commentId and pass that to the Comments.delete Try-it to delete the comment. It will return a 204 status if successful. So that's the concept. Hope this helps.

Related

Is it possible to fetch a channel's about info from Youtube API including (email, description etc)?

Is it possible to fetch links and email for a channel or a user through API?
I looked at the docs, but could not find it there.
One more time YouTube Data API v3 doesn't provide a basic feature.
I would suggest you to use my open-source YouTube operational API, indeed by requesting https://yt.lemnoslife.com/channels?part=about&id=CHANNEL_ID you would get a JSON with the channel about info (description, stats, details and links) you are looking for in item['about'].
Note that the email can't be retrieved automatically as it is protected by a Google captcha as discussed in this StackOverflow question.
Example of result with the YouTube channel id you shared UCp4DtLatjb5dBNHHYcvVXCw:
{
"kind": "youtube#channelListResponse",
"etag": "NotImplemented",
"items": [
{
"kind": "youtube#video",
"etag": "NotImplemented",
"id": "UCp4DtLatjb5dBNHHYcvVXCw",
"about": {
"stats": {
"joinedDate": 1258671600,
"viewCount": "18004212"
},
"description": "FREE Online Piano Lessons, featuring concepts that will greatly assist students in developing a solid and sound technique. Also, this channel features performances by #1 Classical Billboard artist Josh Wright. For questions or video requests, click the link! Josh also offers private online piano lessons through Skype - for more information about how to join his studio, please email him at josh#joshwrightpiano.com",
"details": {
"location": "United States"
},
"links": [
{
"url": "http:\/\/www.joshwrightpiano.com\/contact",
"thumbnail": "https:\/\/encrypted-tbn0.gstatic.com\/favicon-tbn?q=tbn:ANd9GcSI6T1qSJhEF6q1lGXjosLrKLn-8LCOIrI-An-Jq4YgPHlWxdASCMOkA-LB_xpHQyUyaZkglhOVCfhsr6XXlqKwQHIHp9vvDUl-rsS2Q7lHmzuUQTBh1E5qQW6i",
"title": "CLICK TO ASK JOSH A QUESTION"
},
...
{
"url": "https:\/\/plus.google.com\/u\/0\/117288545879529996393\/posts",
"thumbnail": "https:\/\/encrypted-tbn1.gstatic.com\/favicon-tbn?q=tbn:ANd9GcQhmnWcjVxlBhv3BZSHT6S8x7H25cC4vVPKESQba14oYS_WiNOyF2zPPJcHWcseS6lKo4DJ9xWOfAYikH-7n8_VWo9tUcEBHB7It5VEbgYZndFDYgk",
"title": "CLICK TO JOIN JOSH ON GOOGLE+"
}
]
}
}
]
}

No replies or not all comments retrieved using Google YouTube V3 API

I am trying to retrieve all comments to a video with all replies, however, using the Test It interface (or the Java library) I have not been able to retrieve all comments - I have following two examples, when I have failed:
Example 1
The example video and comment is https://www.youtube.com/watch?v=xCLy2DZdXhY&lc=z12ei1s5gs2mc303523qsdigcxmphhlrd04
When I retrieve the comment using
GET https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&id=z12ei1s5gs2mc303523qsdigcxmphhlrd04&fields=etag%2CeventId%2Citems%2Ckind%2CnextPageToken%2CpageInfo%2CtokenPagination%2CvisitorId&key={YOUR_API_KEY}
I receive:
And note the "totalReplyCount": 2, line.
However, when I try to obtain all replies using the parentId:
GET https://www.googleapis.com/youtube/v3/comments?part=snippet&parentId=z12ei1s5gs2mc303523qsdigcxmphhlrd04&fields=etag%2CeventId%2Citems%2Ckind%2CnextPageToken%2CpageInfo%2CtokenPagination%2CvisitorId&key={YOUR_API_KEY}
I receive an empty response:
Remarks
I have read Youtube Data API v3: commentThread call doesn't give replies for some comment threads , however, it does not provide me the answer as I do use the comments list with parentId and I am still not getting any replies.
Even if I try the not recommended way - using the part snippet,replies, I do not get any replies:
Request:
GET https://www.googleapis.com/youtube/v3/commentThreads?part=snippet%2Creplies&id=z12ei1s5gs2mc303523qsdigcxmphhlrd04&fields=etag%2CeventId%2Citems%2Ckind%2CnextPageToken%2CpageInfo%2CtokenPagination%2CvisitorId&key={YOUR_API_KEY}
However, if I do not specify the comment thread by its ID and specify that I want all comment threads for the video:
GET https://www.googleapis.com/youtube/v3/commentThreads?part=snippet%2Creplies&videoId=xCLy2DZdXhY&fields=etag%2CeventId%2Citems%2Ckind%2CnextPageToken%2CpageInfo%2CtokenPagination%2CvisitorId&key={YOUR_API_KEY}
Then voila - The comment thread is now with those two replies:
Why do I receive the replies now and not when I either specify the replies by their parentId or when I specify the comment thread by its id?
Moreover, if I take the (weird) id of one of the replies and try to obtain the comment with this reply, I will receive empty response:
GET https://www.googleapis.com/youtube/v3/comments?part=snippet&id=z12ei1s5gs2mc303523qsdigcxmphhlrd04.1443381718685326&fields=etag%2CeventId%2Citems%2Ckind%2CnextPageToken%2CpageInfo%2CtokenPagination%2CvisitorId&key={YOUR_API_KEY}
Example 2
This problem is a bit different.
I have a video https://www.youtube.com/watch?v=-c76GeR2IWg with 7 comments (6 of them are top levels). When I try to obtain all top level comments related to this video I receive only 4 of them.
GET https://www.googleapis.com/youtube/v3/commentThreads?part=snippet%2Creplies&videoId=-c76GeR2IWg&fields=etag%2CeventId%2Citems%2Ckind%2CnextPageToken%2CpageInfo%2CtokenPagination%2CvisitorId&key={YOUR_API_KEY}
One of the missing comments in the response is https://www.youtube.com/watch?v=-c76GeR2IWg&lc=z120d11g2yyjyxcxw04cg1xbaqfnslfaamk0k .
When I train to obtain comment thread with this id, I do obtain the comment thread:
GET https://www.googleapis.com/youtube/v3/commentThreads?part=snippet%2Creplies&id=z120d11g2yyjyxcxw04cg1xbaqfnslfaamk0k&fields=etag%2CeventId%2Citems%2Ckind%2CnextPageToken%2CpageInfo%2CtokenPagination%2CvisitorId&key={YOUR_API_KEY}
And also, when I try to obtain the replies for this comment (there should be 1 reply) I receive an empty response:
GET https://www.googleapis.com/youtube/v3/comments?part=snippet&parentId=z120d11g2yyjyxcxw04cg1xbaqfnslfaamk0k&fields=etag%2CeventId%2Citems%2Ckind%2CnextPageToken%2CpageInfo%2CtokenPagination%2CvisitorId&key={YOUR_API_KEY}
Remarks
For both examples, the number of comments is less than the size of the page. For this simple example, I have skipped pagination and chose examples with only a few comments, in the real application I use the pagination but I do not get more results.
I do not really understand how is the YouTube and G+ integrated together, thus this might be the issue, however, I was always accesing these videos using only youtube, not checking the users' G+ page, thus I would say that this should not be the case.
Similar questions on SO:
How can I see all the comments with the Youtube API? This is about V2 API, thus no use for me.
Youtube Data API v3: commentThread call doesn't give replies for some comment threads This question is very similar and raises similar problems, however, it is exactly the other way around - The author does not receive all replies using the commentThread replies (which is agreeing with the documentation), however, the proposed solution is "Use the comments.listcall instead and specify the commentThread's ID for the parentId." - which is exactly what does not work for me.
YouTube Data API v3 Comment Thread Discrepency The author forgot about pagination.
YouTube Data API v3 - Comment threads request doesn't return all comments Similar question, yet without any answer.
When I do the following via HTTP request:
https://www.googleapis.com/youtube/v3/comments?part=snippet&parentId=z12ei1s5gs2mc303523qsdigcxmphhlrd04&fields=etag%2CeventId%2Citems%2Ckind%2CnextPageToken%2CpageInfo%2CtokenPagination%2CvisitorId&key={YOUR_API_KEY}
I get the following response:
items": [
{
"kind": "youtube#comment",
"etag": "\"0KG1mRN7bm3nResDPKHQZpg5-do/aOipn7OKd9ibVua9TWdtD2vJJgI\"",
"id": "z12ei1s5gs2mc303523qsdigcxmphhlrd04.1443381718685326",
"snippet": {
"textDisplay": "JM",
"parentId": "z12ei1s5gs2mc303523qsdigcxmphhlrd04",
"authorDisplayName": "Asia Price",
"authorProfileImageUrl": "https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50",
"authorChannelUrl": "http://www.youtube.com/channel/UCtUuxM3_g2hWA7qr17d85RQ",
"authorChannelId": {
"value": "UCtUuxM3_g2hWA7qr17d85RQ"
},
"authorGoogleplusProfileUrl": "https://plus.google.com/100662746258967935686",
"canRate": false,
"viewerRating": "none",
"likeCount": 0,
"publishedAt": "2015-09-27T19:21:58.685Z",
"updatedAt": "2015-09-27T19:21:58.685Z"
}
},
{
"kind": "youtube#comment",
"etag": "\"0KG1mRN7bm3nResDPKHQZpg5-do/NtowtHGdhytzw9YY9RxUopEgoTA\"",
"id": "z12ei1s5gs2mc303523qsdigcxmphhlrd04.1443365800258222",
"snippet": {
"textDisplay": "0",
"parentId": "z12ei1s5gs2mc303523qsdigcxmphhlrd04",
"authorDisplayName": "FAY Fay",
"authorProfileImageUrl": "https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=50",
"authorChannelUrl": "http://www.youtube.com/channel/UC5b4dTxK4ae_roaMWMYpglQ",
"authorChannelId": {
"value": "UC5b4dTxK4ae_roaMWMYpglQ"
},
"authorGoogleplusProfileUrl": "https://plus.google.com/100517618639903741268",
"canRate": false,
"viewerRating": "none",
"likeCount": 0,
"publishedAt": "2015-09-27T14:56:40.258Z",
"updatedAt": "2015-09-27T14:56:40.258Z"
}
}
I get the same results when I use the API explorer.
For your second example,
https://www.googleapis.com/youtube/v3/comments?part=snippet&parentId=z120d11g2yyjyxcxw04cg1xbaqfnslfaamk0k&fields=etag%2CeventId%2Citems%2Ckind%2CnextPageToken%2CpageInfo%2CtokenPagination%2CvisitorId&key={YOUR_API_KEY}
gives me
"items": [
{
"kind": "youtube#comment",
"etag": "\"0KG1mRN7bm3nResDPKHQZpg5-do/UBoqDwv8bg8xZpbIepzI_M5gp9o\"",
"id": "z120d11g2yyjyxcxw04cg1xbaqfnslfaamk0k.1409319325542384",
"snippet": {
"textDisplay": "Ahoj děkuju :) jo máš fajn videa :) ",
"parentId": "z120d11g2yyjyxcxw04cg1xbaqfnslfaamk0k",
"authorDisplayName": "Gumičkování s Péťou",
"authorProfileImageUrl": "https://lh4.googleusercontent.com/-VJce_PtJx70/AAAAAAAAAAI/AAAAAAAAABI/dabMtsy0haY/photo.jpg?sz=50",
"authorChannelUrl": "http://www.youtube.com/channel/UCAyuADHtiVTpiAbt2VVQhtQ",
"authorChannelId": {
"value": "UCAyuADHtiVTpiAbt2VVQhtQ"
},
"authorGoogleplusProfileUrl": "https://plus.google.com/101894467260220798842",
"canRate": false,
"viewerRating": "none",
"likeCount": 0,
"publishedAt": "2014-08-29T13:35:25.542Z",
"updatedAt": "2014-08-29T13:35:25.542Z"
}
}
It might be an issue with your request or API key. Try making a new one and using that.

Given a YouTube video, programmatically get its channel?

I've found at least five Stack Overflow questions of the form "here is a YouTube channel, how do I programmatically obtain the videos on this channel?"
I want to do the opposite: given a video, what channel did it come from?
So far, I've looked for solutions on this in the official YouTube API. I've also looked at libraries like pafy. No luck so far.
https://developers.google.com/youtube/v3/docs/videos/list
It looks like you can make a video request, then set:
part=snippet
This will return the channelId, among other things. I did a request like so:
GET https://www.googleapis.com/youtube/v3/videos?part=snippet&id=f8WsO__XcI0&key={YOUR_API_KEY}
and I get the response:
{
...
"items": [
{
"kind": "youtube#video",
"etag": "\"IHLB7Mi__JPvvG2zLQWAg8l36UU/Xn3r39kJJF0iqtZbqFIeFUTgC0Q\"",
"id": "f8WsO__XcI0",
"snippet": {
"publishedAt": "2015-04-13T21:20:53.000Z",
"channelId": "UC6nSFpj9HTCZ5t-N3Rm3-HA",
...
}

Find the youtube channel id for Google+ development or any other reason

Find the channel id for a youtube channel in case you want to subscribe to it or for any other purpose.
Let's say you want to let the users subscribe to the client application's youtube channel by clicking subscribe. The user would have to sign in to Google+ and then run an api request to subscribe. So for this scenario you would need the channel id.
Please note that the new way of doing things is Google+ instead of GData
Go to the following link and replace the username of that channel[foofightersVEVO with the username of that channel]
https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.search.list?part=id&maxResults=1&q=foofightersVEVO&type=channel&_h=3&
execute the query and look at the api response:
GET https://www.googleapis.com/youtube/v3/search?part=id&maxResults=1&q=foofightersVEVO&type=channel&key={YOUR_API_KEY}
X-JavaScript-User-Agent: Google APIs Explorer
{
"kind": "youtube#searchListResponse",
"etag": "\"2vd4g3cVsHAtTjJSdUMaBo1PBVE/6Ux-QcH6OQdn3t89j3y5z9RQhv0\"",
"pageInfo": {
"totalResults": 161878,
"resultsPerPage": 1
},
"nextPageToken": "CAEQAA",
"items": [
{
"kind": "youtube#searchResult",
"etag": "\"2vd4g3cVsHAtTjJSdUMaBo1PBVE/Q9-widOQXafayJk0l2DV9nrMYO0\"",
"id": {
"kind": "youtube#channel",
"channelId": "UCGRjJrpD2bmk9Ilq6nq80qg"
}
}
]
}
Note the channelId there. Hope this helps.

How to get "transcript" in youtube-api v3

I have started using v3 of the YouTube apis on an android device, using the java client library. Some videos that I am interested in have transcripts that I can access on the web interface (like educational videos). Is there a way to access the transcripts, if present, using the v3 apis?
Thanks
I had the same problem with this... and spent like a week looking for a solution until I hit this:
https://stackoverflow.com/questions/10036796/how-to-extract-subtitles-from-youtube-videos
Just do a GET request on: http://video.google.com/timedtext?lang={LANG}&v={VIDEOID}
You don't need any api/oauth/etc. to access this.
With API v3 you can first grab the available transcripts with the snippet:
https://www.googleapis.com/youtube/v3/captions?videoId=U1e2VNtEqm4&part=snippet&key=(my_api_key):
{
"kind": "youtube#captionListResponse",
"etag": "\"DsOZ7qVJA4mxdTxZeNzis6uE6ck/aGHflncRxq1Uz6m1akhrOLUWUqU\"",
"items": [
{
"kind": "youtube#caption",
"etag": "\"DsOZ7qVJA4mxdTxZeNzis6uE6ck/IC7rNKkn3SQNdovFwR6fEabUYnY\"",
"id": "TqXDnlamg84o4bX0q2oaHz4nfWZdyiZMOrcuWsSLyPc=",
"snippet": {
"videoId": "U1e2VNtEqm4",
"lastUpdated": "2016-01-25T21:50:27.142Z",
"trackKind": "standard",
"language": "en-GB",
"name": "",
"audioTrackType": "unknown",
"isCC": false,
"isLarge": false,
"isEasyReader": false,
"isDraft": false,
"isAutoSynced": false,
"status": "serving"
}
},
{
"kind": "youtube#caption",
"etag": "\"DsOZ7qVJA4mxdTxZeNzis6uE6ck/5UP1qPkmq6mzTUaEVnFC8WqjFgU\"",
"id": "TqXDnlamg84o4bX0q2oaHw_Y53ilUWv6vMFbk0RL3XY=",
"snippet": {
"videoId": "U1e2VNtEqm4",
"lastUpdated": "2016-01-25T21:55:07.481Z",
"trackKind": "standard",
"language": "en-US",
"name": "",
"audioTrackType": "unknown",
"isCC": false,
"isLarge": false,
"isEasyReader": false,
"isDraft": false,
"isAutoSynced": false,
"status": "serving"
}
}
]
}
And then pick the transcript you want:
https://www.googleapis.com/youtube/v3/captions/id?id=TqXDnlamg84o4bX0q2oaHz4nfWZdyiZMOrcuWsSLyPc=
or
https://www.googleapis.com/youtube/v3/captions/TqXDnlamg84o4bX0q2oaHz4nfWZdyiZMOrcuWsSLyPc=
at which point you need provide an authorization key. Apparently a simple key isn't enough. Possibly because:
Quota impact: A call to this method has a quota cost of approximately 200 units.
Note the slight difference in the URLs (/caption/ versus /caption?).
All the lovely documentation is here:
https://developers.google.com/youtube/v3/docs/captions
I may be wrong, but I don't think there is yet a documented way to get the caption track via v3 of the API. If you're authenticating with oAuth2, however, your authentication will also be good for v2 of the API, so you could do a quick call to this feed:
http://gdata.youtube.com/feeds/api/videos/[VIDEOID]/captiondata/[CAPTION TRACKID]
to get the data you want. To retrieve a list of possible caption track IDs with v2 of the API, you access this feed:
https://gdata.youtube.com/feeds/api/videos/[VIDEOID]/captions
That feed request also accepts some optional parameters, including language, max-results, etc. For more details, along with a sample that shows the returned format of the caption track list, see the documentation at https://developers.google.com/youtube/2.0/developers_guide_protocol_captions#Retrieve_Caption_Set
Heres some code I wrote which grabs all the caption tracks from any youtube video without having to use the API. Just plug the video URL in the $video_url variable.
// get video id from url
$video_url = 'https://www.youtube.com/watch?v=kYX87kkyubk';
preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", $video_url, $matches);
// get video info from id
$video_id = $matches[0];
$video_info = file_get_contents('http://www.youtube.com/get_video_info?&video_id='.$video_id);
parse_str($video_info, $video_info_array);
if (isset($video_info_array['caption_tracks'])) {
$tracks = explode(',', $video_info_array['caption_tracks']);
// print info for each track (including url to track content)
foreach ($tracks as $track) {
parse_str($track, $output);
print_r($output);
}
}
Probably the best way is using Youtube API 3. I'm trying it but you need an API key + OAuth 2.0 user. A fast solution is using captionsgrabber and parsing the returned HTML data.
Use example:
https://www.captionsgrabber.com/8302/get-captions.00.php?id=UJTY7ilwSq4
// Where the id is the youtube video id

Resources