我想使用 API 从我的 Youtube 频道中删除视频
这是我的代码:
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);
代码在最后一行中断,说明对象引用未设置为对象。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这似乎是 Google YouTube API 内部的问题。我对良好的请求和视频对象也有同样的问题。 (Google API v1.9.0.0)
更新:查看下面 Claudio 的回复。这是正确的。我收到了支持人员回复的电子邮件,但忘记更新此答案:
此 uri 将失败:
“http://gdata.youtube.com/feeds/api/videos/” + videoID
这将起作用:
“http://gdata.youtube.com/feeds/api/users/”+ 帐户名+“/uploads/”+ 视频ID
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
如果您使用正确的 url(即 /upload feed 中的 url),Delete 方法将按预期工作。
/videos feed 中的条目没有编辑 URL,而发送删除请求必须使用该编辑 URL。我刚刚更新了库(修订版 1169)以返回更有意义的 ArgumentNullException 而不是通用空引用。
请使用此代码删除您上传的视频:
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:
我有以下内容:
I have the following:
这意味着您的 LINQ 查询可能不会返回任何内容,即 null。检查调试器中的
vid
变量,或者更好的是,添加一个 if 条件来查看vid
是否具有有效值。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 whethervid
has a valid value or not.我花了 5 个多小时尝试使用官方示例代码删除视频:
并且我遇到了 410 状态代码的异常。我不知道为什么,但根据
ScottE
回答,此代码删除视频:所以我使用
request.Service.Delete(uri);
除了request.Delete(video );
I spent over 5 hours trying to delete video with official sample code:
and I had and exception with 410 status code. I don't know why but according
ScottE
answer this code deletes video:So I used
request.Service.Delete(uri);
exceptrequest.Delete(video);