2011-04-08 65 views
5

我有一個網站,我已經註冊爲一個Facebook應用程序 - 我現在有一個應用程序ID。使用asp.net發表評論到Facebook牆

我的網站是ASP.net C#。當用戶點擊一個按鈕時,我希望它將預定義的消息發佈到他們的牆上。我期待Facebook向用戶展示登錄對話框 - 他們登錄並授予我的網站應用的發佈權限。

有沒有人有任何示例代碼會這樣做?我認爲我需要使用圖形API,但是我看過的所有示例都使用PHP--我對此一無所知。我正在尋找一個使用Java腳本(我幾乎什麼都不知道)或C#(美麗!)的例子。

*更新*

我設法拿到的access_token。現在我通過Facebook C#API打電話發佈到牆上。我得到的錯誤信息:

(#803),有些要求你不存在的別名:PROFILE_ID

我已經通過API代碼階梯,發現它正試圖發佈到以下地址:{https://graph.facebook.com/PROFILE_ID/feed},發佈數據爲:message = Sample + message + from + c%23 + sdk & access_token = 199209316768200 | 2.1avFTZuDGR4HJ7jPFeaO3Q __。3600.1302897600.1- 100000242760733 | R4DkNDf4JCb6B2F64n5TSQwBqvM

我很確定我的令牌應該是有效的。此前請求訪問令牌我要求publish_stream上的應用程序授權請求如下:

Response.Redirect ("https://www.facebook.com/dialog/oauth?client_id=" + myAppId + "&redirect_uri=" + myURL + "&scope=publish_stream"); 

該SDK代碼,實際上使請求如下:

private string MakeRequest(Uri url, HttpVerb httpVerb, 
            Dictionary<string, string> args) 
     { 
     if (args != null && args.Keys.Count > 0 && httpVerb == HttpVerb.GET) 
     { 
      url = new Uri(url.ToString() + EncodeDictionary(args, true)); 
     } 

     HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; 

     request.Method = httpVerb.ToString(); 

     if (httpVerb == HttpVerb.POST) 
     { 
      string postData = EncodeDictionary(args, false); 

      ASCIIEncoding encoding = new ASCIIEncoding(); 
      byte[] postDataBytes = encoding.GetBytes(postData); 

      request.ContentType = "application/x-www-form-urlencoded"; 
      request.ContentLength = postDataBytes.Length; 

      Stream requestStream = request.GetRequestStream(); 
      requestStream.Write(postDataBytes, 0, postDataBytes.Length); 
      requestStream.Close(); 
     } 

     try 
     { 
      using (HttpWebResponse response 
        = request.GetResponse() as HttpWebResponse) 
      { 
       StreamReader reader 
        = new StreamReader(response.GetResponseStream()); 

       return reader.ReadToEnd(); 
      } 
     } 

任何人都可以看到我在做什麼錯誤?

非常感謝,

Rob。

回答

2

首先,你需要照顧Authentication。您需要創建一個應用程序,並使用OAuth來獲取訪問令牌。所有內容均在「身份驗證指南」中介紹。

要發佈內容到用戶的牆上,請查看發佈下的Graph API

作爲開始,你可以使用Facebook's C# SDK

1

你可以看看使用.NET庫如http://facebooknet.codeplex.com/來做到這一點。有一對夫婦,我只記得這一個...

HTH。

+0

我確實有看看,但文件似乎缺乏。我感覺這可能需要一週的時間來攀登這條學習曲線!我真的需要在本週末有所作爲:| – 2011-04-08 18:50:32

+0

好的,我寫了一篇關於使用Facebook Connect的文章:http://www.devproconnections.com/article/aspnet2/how-to-use-the-facebook-connect-api-in-an-asp-net-web-應用;臉譜團隊http://www.facebook.com/developers也有很好的信息。 – 2011-04-08 19:42:24

+1

感謝Brian,但是現在Connect API已經被Graph API取代了嗎? – 2011-04-11 11:13:54

0

如何嘗試這個API塔我最近開發作出與Facebook整合更容易。

這裏是一個代碼示例爲您的網站上有更多的文檔。

驗證用戶

Imports Branches.FBAPI 
... 
Dim SI As New SessionInfo("[application_id]","applicaiton_secret") 
'Redirects user to facebooks 
SI.AuthenticateUser("http://[my url]", New SessionInfo.PermissionsEnum(){SessionInfo.PermissionsEnum.email, SessionInfo.PermissionsEnum.read_stream})) 
'Called when the user is returned to your page 
Dim FSR = FS.ReadFacebooAuthResponse 
Response.Write(FSR.Access_Token) 
Response.Write(FSR.UserID) 

決策職位

Imports Branches.FBAPI 
... 
Dim SI As New SessionInfo("[access_token]")) 
Dim Posts = New Functions.Posts(SI) 
Dim P As New Post 
P.name = "name of post" 
P.message = "message" 
P.link = "www.cnn.com" 
P.caption = "my caption" 
Posts.PublishCreate("[object ID to post to]", P) 
Dim PostID = P.id 

獲得的東西從圖。

Dim SI As New SessionInfo("[access_token]")) 
Dim Req New Functions.Requests(SI) 
Dim User = Req.GetUserInfo("[optional user ID]") 
Response.Write(U.name) 
1

我創建展示如何做到這一點使用OG視頻:http://www.markhagan.me/Samples/Grant-Access-And-Post-As-Facebook-User-ASPNet

如果你沒有時間看視頻,這裏是全碼:

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Net; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

using Facebook; 

namespace FBO 
{ 
    public partial class facebooksync : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      CheckAuthorization(); 
     } 

     private void CheckAuthorization() 
     { 
      string app_id = "374961455917802"; 
      string app_secret = "9153b340ee604f7917fd57c7ab08b3fa"; 
      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 = 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); 

       client.Post("/me/feed", new { message = "markhagan.me video tutorial" }); 
      } 
     } 
    } 
}