1

我想用c#sdk更新facebookpage。我已經部分成功與此,問題是每當我發佈消息到頁面,帖子是可見的只有管理員(我是頁面的管理員)登錄。我希望每個訪問該頁面的人都能看到帖子或Feed。 (甚至管理員被註銷後的不可見管理員也)如何使用c#sdk作爲管理員將提要發佈到Facebook頁面?

下面的代碼我想實現

public ActionResult FacebookPagePost() 
{ 
      string app_id = "xxxx"; 
      string app_secret = "xxx"; 
      string scope = "publish_stream,manage_pages"; 
      string page_Id = "xxX"; 
      if (Request["code"] == null) 
      { 
       return Redirect(string.Format(
        "https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}", 
        app_id, Request.Url.AbsoluteUri, scope)); 
      } 
      else 
      { 
       try 
       { 

        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('&')) 
         { 
          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 fbAccounts = client.Get("/me/accounts"); 


        dynamic messagePost = new ExpandoObject(); 
        messagePost.picture = "http://pic.com/pic.png"; 
        messagePost.link = "http://www.examplearticle.com"; 
        messagePost.name = "name goes here"; 
        messagePost.description = "description goes here"; 

        //Loop over the accounts looking for the ID that matches your destination ID (Fan Page ID) 
        foreach (dynamic account in fbAccounts.data) { 
         if (account.id == page_Id) 
         { 
          //When you find it, grab the associated access token and put it in the Dictionary to pass in the FB Post, then break out. 
          messagePost.access_token = account.access_token; 
          break; 
         } 
        } 

        client.Post("/" + page_Id + "/feed", messagePost); 
       } 
       catch (FacebookOAuthException ex) 
       { 

       } 
       catch (Exception e) 
       { 

       } 
      } 
} 
+0

有史以來嗎? –

回答

0

1)在創建Facebook應用:developers.facebook.com,並讓自己的應用標識和APPSECRET 。 (有很多的在線教程,因此我會跳過重複它)

2)轉到:http://developers.facebook.com/tools/explorer並從下拉列表中選擇您的應用程序,然後單擊「生成訪問令牌」。

3)然後在這裏執行以下步驟: https://stackoverflow.com/questions/17197970/facebook-permanent-page-access-token爲自己獲得一個永久頁面令牌。 (我不能強調這一點,仔細和徹底地按照步驟)*

*我有工具,我建立這樣做對我來說,我輸入的是APPID,APPSECRET和ACCESSTOKEN,然後該工具生成一個永久頁面令牌爲我。任何人都歡迎使用它,幫助它更好,

https://github.com/devfunkd/facebookpagetokengenerator

============================= ============================================

好的在這一點你應該有你的APPID,APPSECRET永久頁面標記

============================================== ===========================

在Visual Studio解決方案:

4)使用的NuGet:Install-Package Facebook

5)實施的Facebook客戶端

public void PostMessage(string message) 
     { 
      try 
      { 
       var fb = new FacebookClient 
       { 
        AppId = ConfigurationManager.AppSettings.Get("FacebookAppID"), 
        AppSecret = ConfigurationManager.AppSettings.Get("FacebookAppSecret"), 
        AccessToken = ConfigurationManager.AppSettings.Get("FacebookAccessToken") 
       }; 

       dynamic result = fb.Post("me/feed", new 
       { 
        message = message 
       }); 
      } 
      catch (Exception exception) 
      { 
       // Handle your exception 
      } 
     } 

我希望這有助於人誰正在努力想出解決出。

相關問題