Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically unsubscribe a YouTube user C#

Here is my code for a mass unsubscriber I am making; currently everything works - other than the unsubscribe feature.. (Typical huh)

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

(Also available at http://pastebin.com/LnKMYCJp)

When the code "reaches" request.Delete(s) it gives me this error:

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

Does anybody know how to fix this, I have been trying to get this working for around 2 hours and I am still getting this error, no matter what I try

When i used fiddler to find out info about the connection this was the header:

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

However the Google Developers example is this:

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

Hopefully that gives a little heads up :)

like image 348
Connor Spencer Harries Avatar asked Aug 05 '26 18:08

Connor Spencer Harries


1 Answers

After a bit of playing around with the API I think I've found the solution.

The AtomEntry.EditUri needs to be the same as the URI to the individual subscription. As it happens this is already stored in the SubscriptionEntry object (which you're overwriting).

Your code should look something like:

itemToRemove.AtomEntry.EditUri = itemToRemove.SubscriptionEntry.EditUri;

Here's the code I used to test this:

           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();
        }
like image 143
Jamie Dixon Avatar answered Aug 07 '26 07:08

Jamie Dixon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!