[英]How to do a post in facebook on my page fan wall with c# and asp.net
我嘗試了3天后如何用c#創建一個帖子我的粉絲頁面牆,我注意到兩件事情: - Facebook提供沒有更新的文檔,沒有完整和糟糕的例子(經常api更改) - Facebook經常更改他的api和很多帖子是痴迷的。 有人可以糾正我的代碼或提供完整的優秀代碼嗎?
這是我的代碼:
if (String.IsNullOrEmpty(Request.QueryString["code"]))
{
Response.Redirect("https://graph.facebook.com/oauth/authorize?client_id=157873644371673&redirect_uri=http://localhost:2551/Default.aspx&scope=publish_stream,manage_pages,offline_access&display=popup");
}
else
{
FacebookClient fb = new FacebookClient();
dynamic result1 = fb.Get("oauth/access_token", new
{
client_id = "MY_APP_ID",
client_secret = "MY_SECRET_ID",
grant_type = "client_credentials",
redirect_uri = "www.mysite.com"
});
fb.AppId = "MY_APP_ID";
fb.AppSecret = "MY_SECRET_ID";
fb.AccessToken = result1.access_token;
dynamic parameters = new ExpandoObject();
parameters.message = "Check out this funny article";
parameters.link = "http://www.example.com/article.html";
parameters.picture = "http://www.example.com/article-thumbnail.jpg";
parameters.name = "Article Title";
parameters.caption = "Caption for the link";
parameters.description = "Longer description of the link";
parameters.req_perms = "manages_pages";
parameters.scope = "manages_pages";
parameters.actions = new
{
name = "View on Zombo",
link = "www.zombo.com",
};
parameters.privacy = new
{
value = "ALL_FRIENDS",
};
try
{
var result = fb.Post("/" + "MY_FACEBOOK_FAN_PAGE_ID" + "/feed", parameters);
}
catch (FacebookOAuthException ex)
{
//handle something
Response.Write(ex.Message);
}
}
我希望這篇文章能為很多人提供幫助,我試着簡單明了:
1 - 創建您的facebook開發者帳戶,並在您的計算機( localhost
)中測試您的代碼,在“使用facebook身份驗證進行網站身份驗證”字段中設置您的localhost地址。 對我來說,它將是http://localhost:2551/Default.aspx
,例如因為我在我的wweb應用程序的Defaut.aspx中測試。 當你在網站上部署時,你將改變這個地址(對我而言,在我的網站上部署代碼之前,我將使用http://www.mywebsiteurl.com/Default.aspx進行更改)。
2-使用您的Facebook用戶帳戶,創建您的粉絲頁面。
3 - 當您創建粉絲頁面時,請轉到您的粉絲頁面,查看URL以獲取您的PAGE_ID例如我的http://www.facebook.com/pages/toto/446533181408238?ref=ts&fref=ts所以我的PAGE_ID是446533181408238
3-它幾乎完成了,只是一個小小的解釋:因為我創建了粉絲頁面,我是粉絲頁面的管理員,我必須要求自動發布到Facebook發布自我的開發者帳戶,所以我必須得到2個動作的autorisation:publish_stream和manage_pages。
我們去編碼:
private void Do()
{
string app_id = "157873644371675";
string app_secret = "c27a10c347af4280720fa3d76c9ae08c";
string scope = "publish_stream,manage_pages";
if (Request["code"] == null)
{
Response.Redirect(string.Format(
"https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}",
app_id, Request.Url.AbsoluteUri, scope));
}
else
{
Dictionary<string, string> tokens = new Dictionary<string, string>();
string url = string.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&scope={2}&code={3}&client_secret={4}",
app_id, Request.Url.AbsoluteUri, scope, Request["code"].ToString(), app_secret);
HttpWebRequest request = System.Net.WebRequest.Create(url) as HttpWebRequest;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
string vals = reader.ReadToEnd();
foreach (string token in vals.Split('&'))
{
//meh.aspx?token1=steve&token2=jake&...
tokens.Add(token.Substring(0, token.IndexOf("=")),
token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1));
}
}
string access_token = tokens["access_token"];
var client = new FacebookClient(access_token);
dynamic parameters = new ExpandoObject();
parameters.message = "Check out this funny article";
parameters.link = "http://www.natiska.com/article.html";
parameters.picture = "http://www.natiska.com/dav.png";
parameters.name = "Article Title";
parameters.caption = "Caption for the link";
//446533181408238 is my fan page
client.Post("/446533181408238/feed", parameters);
}
}
您通過Graph API請求傳遞的參數存在一些問題。
req_perms
參數並將其替換為scope
, action
字段需要一個數組,而privacy
字段需要一個JSON 。 數組的外觀如下: "actions": {'value': 'CUSTOM', 'allow': '{friend-list-id}'}
。
JSON的外觀如何:
"privacy": [
{
"name": "Comment",
"link": "https://www.facebook.com/1234567890/posts/09876543210987654321"
},
{
"name": "Like",
"link": "https://www.facebook.com/1234567890/posts/09876543210987654321"
}
]
我使用以下代碼將圖像發布到我的Facebook頁面牆
string app_id = "*************";
string app_secret = "*************";
string scope = "publish_actions,manage_pages";
string accessToken = GetAccessToken(FacebookAppId, FacebookAppSecret);
if (Request["code"] == null)
{
Response.Redirect(string.Format("https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}",app_id, Request.Url.AbsoluteUri, scope));
}
else
{
FacebookClient fb = new FacebookClient();
dynamic result1 = fb.Get("oauth/access_token", new
{
client_id = "******",
client_secret = "*************",
grant_type = "publish_actions,manage_pages",
redirect_uri = "*************"
});
fb.AppId = "MY_APP_ID";
fb.AppSecret = "MY_SECRET_ID";
fb.AccessToken = result1.access_token;
// to make new object
dynamic parameters = new ExpandoObject();
parameters.message = "Check out this funny article";
parameters.link = "*************";
parameters.picture = "*************";
parameters.name = "*************";
parameters.caption = "*************";
parameters.description = "*************";
parameters.req_perms = "*************";
parameters.scope = "*************";
parameters.actions = new
{
name = "*************",
link = "*************",
};
parameters.privacy = new
{
value = "*************",
};
try
{
var result = fb.Post("/" + "*************" + "/feed", parameters);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.