[英]how to post in facebook from my asp.net mvc 3 website
我假裝將我的網站與facebook集成,當用戶與Web應用程序交互時,想要自動發布(在特定的Facebook帳戶上)。
有沒有辦法讓這個操作像一個webservice方式?,驗證和調用一個網址,發布我直接在Facebook牆上發送的信息?
我正在使用asp.net mvc3 C#我找到了一個facebook開發者工具包庫,這是正確的啟動方式或我應該怎么做?
什么是必要的只是在Facebook帳戶上自動寫一個帖子,例如當我在我的網站上寫一篇新文章(新聞)時,它將自動發布在fb上。
有什么想讓我開始嗎?
我做了類似的事情,當用戶點擊我的mvc應用程序上的“共享”按鈕時,它會在他的牆上發布一些內容。 使用oauth對話框的問題是,它會將瀏覽器重定向到Facebook站點,供用戶登錄並接受應用程序權限。
在“分享”按鈕上,我將其鏈接到此網址:
<a href=""https://www.facebook.com/dialog/oauth?client_id=[YOUR_APP_ID]&redirect_uri=[THE_REDIRECT_PAGE]/&scope=publish_stream"">
<img src='@Url.Content("~/Images/facebook_share.png")' alt="Share on Facebook!" style="height:28px" />
</a>
YOUR_APP_ID是您的Facebook應用程序ID。 THE_REDIRECT_PAGE是您網站上的公共頁面,一旦用戶登錄並接受了權限,Facebook就會自動重定向。 當facebook重定向時,它會向其附加一個名為“code”的查詢字符串參數。 注意:重定向頁面必須以“/”結尾,它不能以文檔結尾,否則它不起作用!
一旦用戶接受了您的請求,您必須向facebook詢問另一個代碼,稱為訪問代碼,用於在用戶的牆上發布。
此代碼位於重定向頁面上:
public ActionResult Index(string code)
{
string fbAuthCode = Request["code"]; //The authorization code.
string fbAppId = "XXXXXXX"; //Your fb application id.
string fbSecretAppId = "XXXXXXXXXXXXXXXXXXXXX"; //Your fb secret app id, it is found on the fb application configuration page.
string redirectUrl = string.Format("[THE_REDIRECT_PAGE]", locationPointId, entryLocationId); //The redirect url. THIS MUST BE THE EXACT SAME REDIRECT URL USED ON THE JAVASCRIPT LINK!
string fbUrl = "https://graph.facebook.com/oauth/access_token?client_id=" + fbAppId + "&redirect_uri=" + redirectUrl + "&client_secret=" + fbSecretAppId + "&code=" + fbAuthCode; //Url used to post.
string accessToken = string.Empty;
try
{
WebClient client = new WebClient();
using (Stream stream = client.OpenRead(fbUrl))
using (StreamReader reader = new StreamReader(stream))
{
accessToken = reader.ReadToEnd().Split('&')[0].Replace("access_token=", string.Empty);
reader.Close();
}
}
catch (Exception ex)
{
throw new Exception("An error ocurred while trying to get the fb token in " + fbUrl, ex);
}
獲得訪問令牌后,您可以發布到用戶牆:
string postUrl = "https://graph.facebook.com/me/feed";
string postParameters;
postParameters = string.Format("message={0}&picture={1}&name={2}&caption={2}&description={3}&link={4}&access_token={5}",
"[Message]",
"[PictureUrl]",
"[Name]",
"[Caption]",
"[Link]",
accessToken);
try
{
System.Net.WebRequest req = System.Net.WebRequest.Create(postUrl);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(postParameters);
req.ContentLength = bytes.Length;
using (System.IO.Stream os = req.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length); //Push it out there
os.Close();
using (WebResponse resp = req.GetResponse())
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
ViewBag.PostResult = sr.ReadToEnd().Trim();
sr.Close();
}
os.Close();
}
}
catch (Exception ex)
{
throw new Exception("An error ocurred while posting data to the user's wall: " + postUrl + "?" + postParameters, ex);
}
return RedirectToAction(XXXXXXXXXXXxx....); //Then i redirect to another page.
您可以在異常中看到我拋出已發布的URL(用於調試目的)。 使用該URL,您通常可以訪問facebook Graph API Explorer或Linter並檢查實際錯誤。
我不知道這是不是你想要的,但希望它給你一個啟動......我已經掙了幾天這個,因為開放圖上的facebook文檔還不是很好,至少對於我們來說不要用curl :)
https://developers.facebook.com/docs/opengraph/tutorial/ https://developers.facebook.com/docs/opengraph/
希望能幫助到你。 公噸。
這很容易:
第1步:
通過請求“* https://www.facebook.com/dialog/oauth?client_id = YOUR_APP_ID&redirect_uri = YOUR_URL&scope =”publish_stream,email *“獲取有效的Facebook令牌(完整的權限列表: https://developers.facebook。 com / docs / reference / api / user / )
第2步:
將您的消息發布到用戶牆:
curl -F'access_token = ...'\\ -F'消息=您的消息'\\ https://graph.facebook.com/ID_OR_USERNAME/feed
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.