YouTube C# API V3, how do you delete a partially uploaded video without the ID?

孤者浪人 提交于 2019-12-24 03:46:29

问题


The VideoID is returned in the ResponseReceived event but not before the upload has completed. If the user cancels and deletes the upload, how do you delete it from the YouTube servers?

What I'm currently doing is an ugly but working hack.

I set the title to a unique guid. When the upload completes, I set the correct title.

If the upload is deleted, I get the first channel and search the playlist for the guid title. This returns the VideoID and I can delete the video.

There has to be a better way. Can I use the upload_id from the response?


回答1:


Unless someone comes up with a better idea, this is what I'm currently doing and it appears to be working quite well. One caveat is the potential for videos to disappear if they happen to have the same guid tag but I don't think that's very likely.

Some constant strings used

internal const string PartSnippet = "snippet";
internal const string TypeVideo = "video";

Before I kick off the upload, I add a guid tag.

video.Snippet.Tags.Add(upload.UploadId.ToString());

In the ResponseReceived event, when the upload completes, I remove the tag

YouTube.RemoveTag(obj, Upload.UploadId.ToString());

internal static void RemoveTag(Video video, string tag)
{           
    video.Snippet.Tags.Remove(tag);
    var updateRequest = YouTubeService.Videos.Update(video, Constants.PartSnippet);
    updateRequest.ExecuteAsync();
}

If the video needs to be removed from the server, I search for it by guid tag and delete it.

YouTube.DeleteAsyncVideoByGuidTag(Upload.UploadId);

internal async static Task DeleteAsyncVideoByGuidTag(Guid tag)
{
    var listRequest = YouTubeService.Search.List(Constants.PartSnippet);
    listRequest.ForMine = true;
    listRequest.Type = Constants.TypeVideo;
    listRequest.Q = tag.ToString();
    var response = await listRequest.ExecuteAsync();
    foreach (var deleteRequest in response.Items.Select(
        result => YouTubeService.Videos.Delete(result.Id.VideoId)))             
        await deleteRequest.ExecuteAsync();
}

Please note: This is a workaround. It's not the way I'd like to do it but it's the only way I can think of doing it.



来源:https://stackoverflow.com/questions/22369100/youtube-c-sharp-api-v3-how-do-you-delete-a-partially-uploaded-video-without-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!