Add comment to youtube video

时光毁灭记忆、已成空白 提交于 2019-12-13 07:18:39

问题


My webpage is developed in .net and i am trying to add comment to a youtube video using following code.

     string lsDeveloperKey = "myDeveloperKey";

    //This will ask user to login to accounts.google for posting comment
        if (!Request.QueryString.AllKeys.Contains("token"))
        {
            string lsUserName = "myusername";
            string lsPassword = "mypassword";

            YouTubeRequestSettings loSettings = new YouTubeRequestSettings(Keys.PortalName, lsDeveloperKey, lsUserName, lsPassword);
            YouTubeRequest loRequest = new YouTubeRequest(loSettings);

            Uri videoEntryUrl = new Uri(string.Format("{0}/{1}", Google.GData.YouTube.YouTubeQuery.DefaultVideoUri, "ofjQ_Gf5CQc"));
            Google.YouTube.Video loVideo = loRequest.Retrieve<Google.YouTube.Video>(videoEntryUrl);

            string lsRandomVideoId = getRandomId() + loVideo.VideoId;
            Session[lsRandomVideoId] = loVideo; ;

            Response.Redirect(AuthSubUtil.getRequestUrl(Request.Url.ToString() + "?v=" + lsRandomVideoId, "http://gdata.youtube.com", false, true));
        }

    //This will post a comment for logged user
        else
        {
            Session["token"] = AuthSubUtil.exchangeForSessionToken(Request.QueryString["token"], null).ToString();

            YouTubeRequestSettings loSettings = new YouTubeRequestSettings(Keys.PortalName, lsDeveloperKey, (String)            Session["token"]);
            YouTubeRequest loRequest = new YouTubeRequest(loSettings);

            Video loVideo = (Video)Session[Request.QueryString["v"]];

            Comment loComment = new Comment();
            loComment.Content = "This is my comment from my app";
            loRequest.AddComment(loVideo, loComment);
        }
    }

This code execute without any any error. It also posts comment if i log in with "myusername" & "mypassword".

But if i log in with any other user it give me following error: E

Execution of request failed:

https://gdata.youtube.com/feeds/api/videos/ofjQ_Gf5CQc/comments

 Stacktrace: 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.EntrySend(Uri feedUri, AtomBase baseEntry,
 GDataRequestType type, AsyncSendData data) at
 Google.GData.Client.MediaService.EntrySend(Uri feedUri, AtomBase
 baseEntry, GDataRequestType type, AsyncSendData data) at
 Google.GData.Client.Service.Insert(Uri feedUri, AtomEntry newEntry,
 AsyncSendData data) at Google.GData.Client.Service.Insert[TEntry](Uri
 feedUri, TEntry entry) at
 Google.YouTube.YouTubeRequest.AddComment(Video v, Comment c) at
 YouTubeComment.Page_Load(Object sender, EventArgs e)

I have worked a lot on this. I am not sure what wrong here. Any help would be appreciated.


回答1:


Looks like you are using ClientLogin which is deprecated and may not work. Please consider using OAuth2 instead, here is an example: http://code.google.com/p/google-gdata/source/browse/trunk/clients/cs/samples/oauth2_sample/oauth2demo.cs. Also, YouTube will reject certain comments, try with a simple string with no special characters.



来源:https://stackoverflow.com/questions/14646169/add-comment-to-youtube-video

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!