如何使用ASP.Net检查Facebook用户是否喜欢我的脸书页面

时间:2015-05-21 07:21:20

标签: c# asp.net facebook facebook-graph-api

  1. 我想检查一下facebook用户是否喜欢我的脸书页面。我使用javascript获得了很多解决方案,但我想在ASP.Net中实现这个要求。

  2. 我从以下链接复制了代码: http://duanedawnrae.com/Blog/post/2012/02/29/Determine-if-a-Facebook-user-Likes-your-page-with-ASPNET.aspx

  3. 我得到了以下ASP.Net代码,该代码适用于此。

  4. ASP.Net代码:

    public class WebService : System.Web.Services.WebService
    {
        [WebMethod()]
        public string GetFacebookLikeStatus(string fbpageid, string fbappid, string fbtoken, string fburl)
        {
            string strReturn = null;
            // Placeholder for the Facbook "like" API call
            string strURL = null;
            strURL = "https://graph.facebook.com/me/likes?access_token=" + fbtoken;
            // Placeholder for the Facebook GET response
            WebRequest objGETURL = null;
            objGETURL = WebRequest.Create(strURL);
            // Declare response stream
            Stream objStream = null;
            // Declare The Facebook response
            string strLine = null;
            // Declare a count on the search term
            int intStr = 0;
             try
            {
                // Create an instance of the StreamReader
                StreamReader objReader = new StreamReader(objStream);
                // Get the response from the Facebook API as a JSON string.
                // If access_token is not correct for the logged
                // on user Facebook returns (400) bad request error
                objStream = objGETURL.GetResponse().GetResponseStream();
                // If all is well 
                try
                {
                    // Execute the StreamReader
                    strLine = objReader.ReadToEnd().ToString();
                    // Check if Facebook page Id exists or not
                    intStr = strLine.IndexOf(fbpageid);    // if valid return a value
                    if (intStr > 0)
                    {
                        strReturn = "1";
                        // if not valid return a value
                    }
                    else
                    {
                        strReturn = "0";
                    }
                    objStream.Dispose();
                }
                catch (Exception ex)
                {
                    // For testing comment out for production
                    strReturn = ex.ToString();
                    // Uncomment below for production
                    //strReturn = "Some friendly error message"
                }
            }
            catch (Exception ex)
            {
                // For testing comment out for production
                strReturn = ex.ToString();
                // Uncomment below for production
                //strReturn = "Some friendly error message"
            }
            return strReturn;
        }
    }
    
    1. 上面的代码包含一个包含单个函数的Web服务。该函数包含四个输入参数并返回单个输出字符串。

    2. 但是当我运行这个web服务时,我得到了错误,“Value不能为空。参数名称:stream“。此错误即将发生,因为“objStream”变量设置为null。请解决问题,以便我可以得到正确的输出,因为我不知道如何实现我的要求。

1 个答案:

答案 0 :(得分:1)

在Facebook上不允许使用Gating,也没有激励用户喜欢你的网页。用户必须喜欢的东西只是因为他们真的想要,你不能以任何方式奖励他们。

话虽如此,您需要user_likes权限才能使用/me/likes,并且您需要获得Facebook的批准。这不会仅仅是为了检查用户是否喜欢你的页面。

顺便说一句,那篇文章是从2012年开始的。从那时起,很多东西发生了变化。

相关问题