Facebook页面墙上的错误请求(getResponse())

时间:2012-01-26 13:49:53

标签: c# facebook-graph-api .net-4.0 facebook-c#-sdk

当我尝试发布到页面墙时,我收到错误代码400“错误请求”。这是代码:

public string Post(Int64 Id)
{

    using (DataContext db = new DataContext())
    {
        var msg = (from t1 in db.Table1
                   join t2 in db.Table2 on t1.UserId equals t2.UserId
                   where t1.Id == Id
                   select new {t1, t2}).FirstOrDefault();

        var url = "https://graph.facebook.com/" + msg.t2.Table3.FBPageId + "/feed";
        AppendQueryString(ref url, "access_token", msg.t2.Table3.FacebookAuth);
        AppendQueryString(ref url, "privacy", "{\"value\": \"EVERYONE\"}");
        AppendQueryString(ref url, "message", msg.t1.Message);
        var webRequest = WebRequest.Create(url);
        webRequest.ContentType = "application/x-www-form-urlencoded";
        webRequest.Method = "POST";
        byte[] bytes = System.Text.Encoding.ASCII.GetBytes(url);
        webRequest.ContentLength = bytes.Length;
        System.IO.Stream os = webRequest.GetRequestStream();
        os.Write(bytes, 0, bytes.Length);
        os.Close();

        try
        {
            var webResponse = webRequest.GetResponse();
        }
        catch (Exception ex)
        {
            return ex.Stacktrace;
        }

    return "Something random";             
}  

代码以catch中的返回结束(webRequest.GetResponse();失败)。

AppendQueryString只是一种生成查询字符串(?或&)并返回新url的方法。

我做错了吗?

1 个答案:

答案 0 :(得分:0)

我不得不将“基本”网址和参数分成两部分,并将参数放入byte[] bytes = System.Text.Encoding.ASCII.GetBytes(parameters);并将网址保留在var webRequest = WebRequest.Create(url);

立即投放