这是我正在制作的大规模取消订阅者的代码,目前一切正常 - 除了取消订阅功能..(典型的呵呵)
public void UnSubUsers()
{
string feedUrl = "http://gdata.youtube.com/feeds/api/users/" + username.Text + "/subscriptions";
YouTubeQuery query = new YouTubeQuery(feedUrl);
subFeed = service.GetSubscriptions(query);
YouTubeRequestSettings yts = new YouTubeRequestSettings("Unsubscriber", DEVKEY, username.Text, password.Text);
YouTubeRequest request = new YouTubeRequest(yts);
int i = 0;
int x = 0;
x = (listBox1.Items.Count);
for (i=0;i<x ;i++ )
{
string uname = listBox1.Items[i].ToString();
uname=uname.Substring(42);
uname = uname.Remove(uname.LastIndexOf("/"));
Subscription s = new Subscription();
s.Type = SubscriptionEntry.SubscriptionType.channel;
s.UserName = uname;
//MessageBox.Show(uname); //Displays the username so that we know if it is correct
try
{
s.AtomEntry.EditUri = "http://gdata.youtube.com/feeds/api/users/" + username.Text + "/subscriptions";
s.SubscriptionEntry.EditUri = "http://gdata.youtube.com/feeds/api/users/" + username.Text + "/subscriptions";
request.Delete(s);
}
catch (ArgumentNullException e)
{
MessageBox.Show(e.ToString(), "Error");
}
catch (GDataRequestException e)
{
MessageBox.Show(e.ToString(), "Error");
}
}
}
(也可在http://pastebin.com/LnKMYCJp获得)
当代码“到达”request.Delete(s)时,它会给我这个错误:
Google.GData.Client.GDataRequestException: Execution of request failed: http://gdata.youtube.com/feeds/api/users/iWinterHD/subscriptions --->System.Net.WebException: The remote server returned an error: (400) Bad Request.
at System.Net.HttpWebRequest.GetResponse()
at Google.GData.Client.GDataRequest.Execute()
--- End of inner exception stack trace ---
at Google.GData.Client.GDataRequest.Execute()
at Google.GData.Client.GDataGAuthRequest.Execute(Int32 retryCounter)
at Google.GData.Client.GDataGAuthRequest.Execute()
at Google.GData.Client.Service.Delete(Uri uriTarget, String eTag)
at Google.GData.Client.FeedRequest1.Delete[Y](Y entry)
at Unsubscriber.SubForm.UnSubUsers() in C:\Users\iWinterHD\documents\visual studio 2010\Projects\Unsubscriber\Unsubscriber\SubForm.cs:line 112
有没有人知道如何解决这个问题,我一直试图让这个工作大约2个小时,无论我尝试什么,我仍然会收到此错误
当我使用fiddler查找有关连接的信息时,这就是标题:
DELETE /feeds/api/users/iWinterHD/subscriptions HTTP/1.1
Content-Type: application/atom+xml; charset=UTF-8
User-Agent: G-Unsubscriber/GDataGAuthRequestFactory-CS-Version=2.1.0.0--IEnumerable
X-GData-Key: key=DEVELOPER_KEY
Authorization: GoogleLogin auth=DQAAAMgAAAAfAWmos6z7rpaY8JrK2RNK4Urf7Riu_putKeGgV1KFH5OEmAYA2t5w0DWXbVQJnizQiPmLSl-4D0eCozYn5jVp4DWs4Rpao3udc3eTIC9wibBGRe640m7zZjl96UnFMyf-fJDk0VrTIcAw74S7_WhwBaRDjLS77EOWfERw066NmcYO-2QB_6WZ4Y0o3Y4haVn_pRokm8ckyuTRWJf6cES1yVlZ4fP5diUySVsH7EaHLiUcAquUl7GWCMdF_JbjRVVxvgeMW1zV757JW8l841uk
GData-Version: 2.0
Host: gdata.youtube.com
Connection: Keep-Alive
然而,Google Developers的例子就是:
DELETE /feeds/api/users/default/subscriptions/SUBSCRIPTION_ID HTTP/1.1
Host: gdata.youtube.com
Content-Type: application/atom+xml
Authorization: Bearer ACCESS_TOKEN
GData-Version: 2
X-GData-Key: key=DEVELOPER_KEY
希望这有点让人头晕目眩:)
答案 0 :(得分:1)
在玩了一些API之后,我想我已经找到了解决方案。
AtomEntry.EditUri
需要与单个订阅的URI相同。碰巧这已经存储在SubscriptionEntry
对象中(你要覆盖它)。
您的代码应该类似于:
itemToRemove.AtomEntry.EditUri = itemToRemove.SubscriptionEntry.EditUri;
以下是我用来测试的代码:
var subscriptionsUrl =
"http://gdata.youtube.com/feeds/api/users/warmthonthesoul/subscriptions";
var settings = new YouTubeRequestSettings([...]);
var request = new YouTubeRequest(settings);
var query = new YouTubeQuery(subscriptionsUrl);
var feed = request.GetSubscriptionsFeed("warmthonthesoul").Entries;
var itemToRemove = feed.SingleOrDefault(x =>
x
.SubscriptionEntry
.Title.Text.Contains("Triforcefilms"));
if(itemToRemove != null)
{
itemToRemove.AtomEntry.EditUri = itemToRemove
.SubscriptionEntry
.EditUri;
request.Delete(itemToRemove);
Console.WriteLine("Item removed");
}
Console.ReadLine();
}
答案 1 :(得分:1)
在搜索了所有变量大约4个小时后,我最终绊倒了ID变量,我后来发现需要将其传递到最终URL以便删除带有该ID的订阅,我测试了它并且它工作了完美!
public void ListSubs()
{
string feedUrl = "http://gdata.youtube.com/feeds/api/users/" + username.Text + "/subscriptions";
YouTubeQuery query = new YouTubeQuery(feedUrl);
try
{
subFeed = service.GetSubscriptions(query);
foreach (SubscriptionEntry entry in subFeed.Entries)
{
string id = entry.Id.AbsoluteUri;
id = id.Substring(id.LastIndexOf(":")+1);
listBox1.Items.Add(id);
string usrname = entry.Content.Src.Content;
usrname = usrname.Substring(42);
usrname = usrname.Remove(usrname.LastIndexOf("/"));
listBox2.Items.Add(usrname);
}
}
catch(GDataRequestException e)
{
MessageBox.Show(e.ToString(), "Error:");
}
}
public void UnSubUsers()
{
YouTubeRequestSettings yts = new YouTubeRequestSettings("Unsubscriber", DEVELOPER_KEY, username.Text, password.Text);
YouTubeRequest request = new YouTubeRequest(yts);
int i = 0;
int x = 0;
x = (listBox1.Items.Count);
for (i=0;i<x ;i++ )
{
string uname = listBox1.Items[i].ToString();
yts = new YouTubeRequestSettings("Unsubscriber", DEVELOPER_KEY, username.Text, password.Text);
request = new YouTubeRequest(yts);
Subscription s = new Subscription();
s.Type = SubscriptionEntry.SubscriptionType.channel;
s.UserName = uname;
s.Id = listBox1.Items[i].ToString()
try
{
s.AtomEntry.EditUri = "http://gdata.youtube.com/feeds/api/users/" + username.Text + "/subscriptions" + "/" + listBox1.Items[i].ToString();
request.Delete(s);
}
catch (ArgumentNullException e)
{
}
catch (GDataRequestException e)
{
}
}
}
我不得不将订阅ID添加到我用来删除订阅的URL中,这里是我的代码,用于将订阅ID添加到我最初用于存储用户名的列表框中,但事实证明你无法将用户名传递给删除方法,但这也适用,因为我添加了第二个列表框来查找订阅ID的用户名
string id = entry.Id.AbsoluteUri;
id = id.Substring(id.LastIndexOf(":")+1);
listBox1.Items.Add(id);
此代码从条目变量中获取订阅ID,然后将订阅ID添加到EditUri变量:
s.AtomEntry.EditUri = "http://gdata.youtube.com/feeds/api/users/" + username.Text + "/subscriptions" + "/" + listBox1.Items[i].ToString();
request.Delete(s);
我的群发取消订阅者现已完成!
非常感谢@JamieDixon的所有精彩帮助!