Lakshay Dulani
Lakshay Dulani

Reputation: 1776

I want to delete a video from my channel on Youtube using the API

This is my code:

YouTubeService serv = new YouTubeService("myDeleteService", YOUTUBE_DEVELOPER_KEY);            
serv.setUserCredentials(USERNAME, PASSWORD);
YouTubeRequestSettings settings = new YouTubeRequestSettings(YOUTUBE_CHANNEL, YOUTUBE_DEVELOPER_KEY);
YouTubeRequest request = new YouTubeRequest(settings);
string feedUrl = String.Format("http://gdata.youtube.com/feeds/api/users/{0}/uploads", YOUTUBE_CHANNEL);
Feed<Video> videoFeed = request.Get<Video>(new Uri(feedUrl));
Uri videoEntryUrl = new Uri("http://gdata.youtube.com/feeds/api/videos/" + VideoId);
Video video = request.Retrieve<Video>(videoEntryUrl);
Video vid = (from vi in videoFeed.Entries
             where vi.VideoId == VideoId
             select vi).First<Google.YouTube.Video>();
request.Delete(vid);

The code breaks on the last line stating that object reference is not set to an object.

Upvotes: 5

Views: 1863

Answers (5)

Chaki_Black
Chaki_Black

Reputation: 932

I spent over 5 hours trying to delete video with official sample code:

YouTubeRequestSettings settings = new YouTubeRequestSettings(YOUTUBE_CHANNEL, YOUTUBE_DEVELOPER_KEY, USERNAME, PASSWORD);
YouTubeRequest request = new YouTubeRequest(settings);
Uri videoEntryUrl = new Uri(String.Format("http://gdata.youtube.com/feeds/api/users/{0}/uploads/{1}", YOUTUBE_CHANNEL, VIDEO_ID));
Video video = request.Retrieve<Video>(videoEntryUrl);
request.Delete(video);

and I had and exception with 410 status code. I don't know why but according ScottE answer this code deletes video:

YouTubeRequestSettings settings = new YouTubeRequestSettings(YOUTUBE_CHANNEL, YOUTUBE_DEVELOPER_KEY, USERNAME, PASSWORD);
YouTubeRequest request = new YouTubeRequest(settings);
Uri uri = new Uri(String.Format("http://gdata.YouTube.com/feeds/api/users/default/uploads/{0}", videoId));
request.Service.Delete(uri);

So I used request.Service.Delete(uri); except request.Delete(video);

Upvotes: 0

joeydee
joeydee

Reputation: 127

This appears to be an issue internal to the Google YouTube API. I'm having the same issue with good request and video objects. (Google API v1.9.0.0)

UPDATE: check out Claudio's response below. It is correct. I received an email back from support and forgot to update this answer:

this uri will fail: "http://gdata.youtube.com/feeds/api/videos/" + videoID

this will work: "http://gdata.youtube.com/feeds/api/users/" + accountName + "/uploads/" + videoID

Upvotes: 4

Claudio Cherubino
Claudio Cherubino

Reputation: 15024

The Delete method works as expected if you use the right url, i.e. the one from the /upload feed.

The entries in the /videos feed do not have an edit url which is the one that must be used to send a delete request. I just updated the library (rev. 1169) to return a more meaningful ArgumentNullException instead of the generic null reference.

Please use this code to delete a video you uploaded:

YouTubeRequestSettings settings = new YouTubeRequestSettings(YOUTUBE_CHANNEL, YOUTUBE_DEVELOPER_KEY, USERNAME, PASSWORD);
YouTubeRequest request = new YouTubeRequest(settings);
Uri videoEntryUrl = new Uri(String.Format("http://gdata.youtube.com/feeds/api/users/{0}/uploads/{1}", YOUTUBE_CHANNEL, VIDEO_ID));
Video video = request.Retrieve<Video>(videoEntryUrl);
request.Delete(video);

Upvotes: 3

ScottE
ScottE

Reputation: 21630

I have the following:

CreateAuthenticatedRequest().Service.Delete(new Uri(GetVideoUploadUrl(videoId)));

    public static YouTubeRequest CreateAuthenticatedRequest()
    {
        YouTubeRequestSettings settings = new YouTubeRequestSettings(ConfigurationManager.AppSettings["GData.AppName"],  ConfigurationManager.AppSettings["GData.DeveloperKey"], ConfigurationManager.AppSettings["GData.Email"], ConfigurationManager.AppSettings["GData.Password"]);
        settings.Timeout = 1000000;
        return new YouTubeRequest(settings);
    }

    private static string GetVideoUploadUrl(string videoId)
    {
        return string.Format("http://gdata.youtube.com/feeds/api/users/default/uploads/{0}", videoId);
    }

Upvotes: 1

Aamir
Aamir

Reputation: 15576

This means that your LINQ query is probably returning nothing i.e., null. Check the vid variable in debugger or better yet, put an if condition to see whether vid has a valid value or not.

Upvotes: 0

Related Questions