JohnTranstel / google-gdata

Automatically exported from code.google.com/p/google-gdata
0 stars 0 forks source link

Adding comments with the Youtube API randomly failing in .NET #582

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
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. 

Original issue reported on code.google.com by Sir.d00...@gmail.com on 13 Apr 2012 at 10:10

GoogleCodeExporter commented 8 years ago
Your code above is performing authentication twice.
First, you are doing ClientLogin to authorize the YouTubeService instance, 
however, the rest of the code doesn't use that service or the auth token at all.

The YouTubeRequest instance will also perform ClientLogin when you try to add a 
comment, so you can drop the service class entirely (assuming you are not using 
it somewhere else).

Perhaps the YouTube API sees the two authentication requests coming in a short 
period of time and doesn't allow them.
Remove the first authentication request and see if that helps.
If not, use a tool like Fiddler to capture one of those failed requests and 
share the log with us so that we can investigate.

Original comment by ccherub...@google.com on 13 Apr 2012 at 7:00

GoogleCodeExporter commented 8 years ago
I seemed to have resolved the issue now, the two authentications was not the 
issue as they don't happen within quick succesion and the first is used really 
just to check that it is a valid log in although I have actually changed my 
code now to only authenticate once. I noticed in the response string that it 
was saying "posting too fast" after trying to put a second comment in, I do not 
know the default time space exactly between comments but making users wait 1 
minute to post another comment was to little and 2 minutes seems to work fine. 
Do you know the exact timings?

Original comment by Sir.d00...@gmail.com on 19 Apr 2012 at 8:45

GoogleCodeExporter commented 8 years ago
Glad to hear you found the problem.
I don't know the YouTube API limits, you can try asking in the dedicated forum:

https://groups.google.com/group/youtube-api-gdata?pli=1

Original comment by ccherub...@google.com on 19 Apr 2012 at 4:22