Search code examples
youtube-apiyoutube-data-apiandroid-youtube-api

Delete videos from playlist using YouTube API


I'm trying to delete a video from one of my playlists using the YouTube API. I'm properly authenticated but I'm still getting the following error:

{
    "code" : 403,
    "errors" : [ {
    "domain" : "youtube.playlistItem",
    "location" : "id",
    "locationType" : "parameter",
    "message" : "Forbidden",
    "reason" : "playlistItemsNotAccessible"
    } ],
"message" : "Forbidden"
}

I'm following the instructions here: https://developers.google.com/youtube/v3/docs/playlistItems/delete

But what I don't understand is where you're supposed to put the playlistID. I see where you put the videoID, but how does it know which playlist to delete from? I think that's my problem. Here is the code in their example, and mine is identical:

// Sample java code for playlistItems.delete

public static void main(String[] args) throws IOException {
    YouTube service = getYouTubeService();
    try {
        HashMap<String, String> parameters = new HashMap<>();
        parameters.put("id", "REPLACE_ME");
        parameters.put("onBehalfOfContentOwner", "");

        YouTube.PlaylistItems.Delete playlistItemsDeleteRequest = youtube.playlistItems().delete(parameters.get("id").toString());
        if (parameters.containsKey("onBehalfOfContentOwner") && parameters.get("onBehalfOfContentOwner") != "") {
            playlistItemsDeleteRequest.setOnBehalfOfContentOwner(parameters.get("onBehalfOfContentOwner").toString());
        }

        playlistItemsDeleteRequest.execute();
    }
}

There's not even an input for the playlistID in their "try it" section on the page, which also gives the same error. Just onBehalfOfContentOwner and id. I get the same error after putting in a videoID and executing it on the page. Where should I put the playlistID?


Solution

  • Figured it out. And to clarify: I was trying to delete a video from my own playlist and I was properly authenticated (I could add videos just fine).

    Basically, I was using the wrong videoId. I was trying to use the short one you see in the url when you play a video (e.g. qNqfYtd3HTg). You need to use the one that comes back from PlaylistItems.list instead (e.g. UEwzdmpFaWdSbm5rQ3hPN29qNXFjM1c0c20zNVlRSC1hQi5DNUEzOUFFNkIyOUUzOTRC). The latter includes the information about which playlist the video is in. That's why you don't need to specify the playlistId when deleting a video from a playlist, just this one long videoId.

    This is the videoId NOT to use when deleting a video from your playlist:

    youtube.playlistItems().list("contentDetails,snippet").execute().items[0].snippet.resourceId.videoId

    And this is the videoId to use:

    youtube.playlistItems().list("contentDetails,snippet").execute().items[0].id