2012-01-03 24 views
5

Đây là mã của tôi:Tôi muốn xóa một đoạn video từ kênh của tôi trên Youtube bằng cách sử dụng API

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); 

Các phá vỡ mã trên dòng cuối cùng tuyên bố rằng đối tượng tham chiếu không được thiết lập để một đối tượng.

Trả lời

4

Điều này có vẻ là sự cố bên trong API Google YouTube. Tôi gặp vấn đề tương tự với yêu cầu và đối tượng video tốt. (Google API v1.9.0.0)

CẬP NHẬT: xem phản hồi của Claudio bên dưới. Đung vậy. Tôi nhận được một email trở lại từ hỗ trợ và quên để cập nhật câu trả lời này:

uri này sẽ thất bại: "http://gdata.youtube.com/feeds/api/videos/" + videoid

chí này công việc: "http://gdata.youtube.com/feeds/api/users/" + accountName + "/ uploads /" + videoid

+0

Bất kỳ giải pháp nào? Tôi đã gặp phải vấn đề tương tự. –

+0

Tôi cũng nhận được lỗi này. Giải pháp của bạn thực sự giúp tôi. +1 giúp bạn – Sukhjeevan

0

Điều này có nghĩa là truy vấn LINQ của bạn có thể không trả về gì, tức là, không có. Kiểm tra biến số vid trong trình gỡ rối hoặc tốt hơn, đặt điều kiện if để xem liệu vid có giá trị hợp lệ hay không.

+0

Nếu dòng cuối cùng là lỗi ở đâu, và nó cho anh ta một ngoại lệ null thì nó cũng có thể đối tượng yêu cầu là null. Nó sẽ là chương trình nghèo để cho phép một ngoại lệ xảy ra chỉ vì Video là null. –

1

tôi có như sau:

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); 
    } 
3

phương pháp Xóa hoạt động như mong đợi nếu bạn sử dụng đúng url, tức là từ url/phí tải lên d.

Các mục nhập trong nguồn cấp dữ liệu/video không có url chỉnh sửa là url phải được sử dụng để gửi yêu cầu xóa. Tôi vừa cập nhật thư viện (rev. 1169) để trả về một ArgumentNullException có ý nghĩa hơn thay vì tham chiếu null chung chung.

Vui lòng sử dụng mã này để xóa một video mà bạn tải lên:

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); 
0

tôi đã dành hơn 5 giờ cố gắng để xóa video với mẫu mã chính thức:

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); 

và tôi đã và ngoại lệ với 410 mã trạng thái. Tôi không biết tại sao nhưng theo ScottE câu trả lời mã này xóa 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); 

Vì vậy, tôi sử dụng request.Service.Delete(uri); trừ request.Delete(video);

Các vấn đề liên quan