Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding comments with the Youtube API randomly failing in .NET

I am using a C# .NET 2.0 winform in 2010, I have added the ability for a user to log in and post comments. I copied the .NET developer guide in how to post comments but I am getting random but frequent exceptions when trying to post comments. At first I thought it might be because there is some issue with using a google e-mail instead of the youtube log in name, to get around this when a user succesfully logs in I request the profile, get the user name and create a new youtube settings class and give the appropriate credentials with the users profile name. This however hasn't resolved the issue, the comments still work sporadically. Here is the code that basically handles logging in.

       youtubeService.setUserCredentials(userBox.Text, passwordBox.Text);

        try
        {
            String strAuth = youtubeService.QueryClientLoginToken();
        }
        catch (Exception ex)
        {
        }         

The above code is in a seperate form, the form that hosts the youtube video basically looks to see if this process has been completed and grabs the username, password used to log in and sets the new settings:

                m_LoggedInSettings = new YouTubeRequestSettings(myappname, mydevkey, username, password);
                m_LoggedInRequest = new YouTubeRequest(m_LoggedInSettings); 

This then used to add a comment:

                Comment userComment = new Comment();
                userComment.Content = commentText;
                m_LoggedInRequest.AddComment(youtubevideo, userComment);   

When it fails I get the following:

{"Execution of request failed: https://gdata.youtube.com/feeds/api/videos/t-8K8Hj8bxE/comments"}

With the following info:

{"The remote server returned an error: (403) Forbidden."}

Status code:

System.Net.HttpStatusCode.Forbidden

Status description:

Forbidden

A few things come to mind, I do not have a proper log out that sends anything to youtube implimented at the minute (is this needed?), so it may be that I've logged in multiple times and that is somehow flagging on youtubes side? It could also be that I am essentially creating new settings and request objects that weren't used to get the video/comments and maybe the video taken from the normal settings file (with no log in) is giving problems or something like that? To be honest, I haven't got a clue what is wrong and any help would be greatly appriecated.

like image 295
user1105748 Avatar asked Nov 13 '22 07:11

user1105748


1 Answers

Okay, so I figured out that the reason they were erroring is because I was trying to post consecutive comments too fast, however I don't know what the timeout is for being able to post more comments.

like image 98
user1105748 Avatar answered Nov 16 '22 04:11

user1105748