使用c#和HttpWebRequest发布打开的图形操作

时间:2012-08-17 16:39:17

标签: c# facebook httpwebrequest

大家好我试图使用HttpWebRequest方法向Facebook发布一个开放的图形动作。

这是我的请求方法

public static string RequestUrl(string action, String HTTPMETHOD, dynamic postdata = null)
    {
        string results = "";

        try
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(action);
            if (HTTPMETHOD == "GET")
            {
                req.Method = WebRequestMethods.Http.Get;
            }
            else if (HTTPMETHOD == "POST")
            {
                ASCIIEncoding encoding = new ASCIIEncoding();
                req.Method = WebRequestMethods.Http.Post;
                byte[] data = encoding.GetBytes(postdata.sneaqer);
                req.ContentLength = data.Length;
                req.ContentType = "application/x-www-form-urlencoded";
                Stream newStream = req.GetRequestStream();
                newStream.Write(data, 0, data.Length);
                newStream.Close();
            }
            else if (HTTPMETHOD == "DELETE")
            {
                req.Method = "DELETE";
                ASCIIEncoding encoding = new ASCIIEncoding();
            }
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            StreamReader sr = new StreamReader(resp.GetResponseStream());
            results = sr.ReadToEnd();
            sr.Close();
        }
        catch (Exception ex)
        {
            Error.Log("ERROR: Common.cs requestUrl() " + ex.Message + " " + action);
        }

        return results;
    }

这是我到目前为止所尝试的

var url = "https://graph.facebook.com/" + personFacebookUserId + "/verbNamespace:follow";
    dynamic parameters = new System.Dynamic.ExpandoObject();
    parameters.person = "http:" + Configuration.getConfigValue("SiteUrl") + "OG/OpenGraphAction.aspx?type=follow&facebookProfilePicture=" + friendFacebookUserId;
    string result = Common.RequestUrl(url, "POST", parameters);

我收到错误服务器返回错误请求。我认为问题是我传递参数的方式。人是对象,跟随是行动。

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

我发现这篇文章非常有用Using ASP.Net with Facebook’s Graph API and OAuth 2.0 Authentication似乎你必须通过让他们重定向到你的某个页面来获得身份验证令牌。

相关问题