Facebook Graph API 发布到墙上
本文关键字:Graph API Facebook | 更新日期: 2023-09-27 18:32:23
我正在尝试使用这种方法将照片发布到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;
}
我遇到的问题是帖子数据。我无法让它工作,我不确定那个字符串会是什么样子?谁能举个例子?
你需要发布到 https://graph.facebook.com/me/feed?access_token=YOUR_ACCESS_TOKEN,发布的数据应如下所示
消息=这是测试消息
(您应该使用 Uri.EscapeDataString 转义消息中的特殊字符,但不要转义"=")
您还可以指定其他属性(如文档中所定义),例如:
消息={此处转义消息}&链接={此处的转义链接}&标题={此处转义标题文本}&标题={此处转义标题文本}&描述={此处转义描述文本}&图片={Urlen编码图片网址}
(当然没有大括号。这会在您的帖子中嵌入一个链接,其中包含您选择的标题、标题、描述和缩略图。
我们看不到您的WebResponseGet
方法,但您应该在发布不成功时检查 responseData 包含的内容。或者,在 WebResponseGet 中查找 HTTP 状态代码和 WebException。它有助于读取返回的数据;Facebook 以如下 JSON 格式返回错误(示例):
{"错误":{"消息":"缺少消息或 attachment.","type":"FacebookApiException","code":100,"error_subcode":1349125}}
请注意,发布需要应用程序的publish_actions
权限(请求访问令牌时需要将其添加到范围,如果在请求令牌时不在范围内,则需要新令牌)。