Facebook Graph API发布到墙上

时间:2012-04-26 11:12:42

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

我正在尝试使用此方法将照片发布到Facebook墙上。

/// <param name="method">Http Method</param>
/// <param name="url">Full url to the web resource</param>
/// <param name="postData">Data to post in querystring format</param>
/// <returns>The web server response.</returns>

public string WebRequest(Method method, string url, string postData)
{
    HttpWebRequest webRequest = null;
    StreamWriter requestWriter = null;
    string responseData = "";
    webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
    webRequest.Method = method.ToString();
    webRequest.ServicePoint.Expect100Continue = false;
    webRequest.UserAgent = "[]";
    webRequest.Timeout = 20000;

    if (method == Method.POST)
    {
        webRequest.ContentType = "application/x-www-form-urlencoded";

        requestWriter = new StreamWriter(webRequest.GetRequestStream());
        try
        {
            requestWriter.Write(postData);
        }
        catch
        {
            throw;
        }
        finally
        {
            requestWriter.Close();
            requestWriter = null;
        }
    }
    responseData = WebResponseGet(webRequest);
    webRequest = null;
    return responseData;
}

我遇到的问题是postData。我无法让它工作,我不确定那个字符串会是什么样子?谁能举个例子?

1 个答案:

答案 0 :(得分:0)

您需要发布到https://graph.facebook.com/me/feed?access_token=YOUR_ACCESS_TOKEN, 发布的数据应该看起来像

  

message =这是一条测试消息

(你应该使用Uri.EscapeDataString转义邮件中的特殊字符,但不要逃避&#39; =&#39;)

您还可以指定其他属性(如documentation中所定义),例如:

  

message = {此处有转义邮件}&amp; link = {urlencoded link here}&amp; caption = {escapeped caption text here}&amp; title = {escapeped title text here}&amp; description = {escapeped description text here}&amp; ; picture = {urlencoded picture url}

(当然没有花括号。)这会在你的帖子中嵌入一个链接,其中包含你选择的标题,标题,描述和缩略图。

我们无法看到您的WebResponseGet方法,但您应该检查发布未成功时responseData包含的内容。或者,在WebResponseGet中查找HTTP状态代码和WebExceptions。它有助于读取返回的数据; facebook以JSON格式返回错误(例如):

  

{&#34;错误&#34;:{&#34;消息&#34;:&#34;缺少消息或   。连接&#34;&#34;类型&#34;:&#34; FacebookApiException&#34;&#34;代码&#34;:100,&#34; error_subcode&#34;:1349125}}

请注意,发布需要您的应用程序的publish_actions权限(您需要在请求访问令牌时将其添加到范围中,如果您在请求令牌时未在范围内,则您需要一个新的令牌。)