2015-04-06 54 views
0

沒有太多麻煩,我設法在我的個人Facebook上發佈狀態消息。但我想要實現的是在我的頁面上發佈消息作爲頁面。作爲頁面發佈MVC Facebook

public class Facebook 
     { 
      private const string FacebookApiId = "xxxxxxxxxxxxxxx"; 
      private const string FacebookApiSecret = "xxxxxxxxxxxxxxxx"; 
      string scope = "publish_stream,manage_pages"; 


      private const string AuthenticationUrlFormat = "https://graph.facebook.com/oauth/access_token?client_id={0}&client_secret={1}&grant_type=client_credentials&scope=publish_stream,manage_pages"; 


      static string GetAccesToken(string apiId, string apiSecret) 
      { 

       string url = string.Format(AuthenticationUrlFormat, apiId, apiSecret); 
       string accesToken = string.Empty; 
       WebRequest request = WebRequest.Create(url); 
       WebResponse response = request.GetResponse(); 

       using (Stream responseStream = response.GetResponseStream()) 
       { 
        StreamReader reader = new StreamReader(responseStream, Encoding.UTF8); 
        String responseString = reader.ReadToEnd(); 

        NameValueCollection query = HttpUtility.ParseQueryString(responseString); 

        accesToken = query["access_token"]; 
       } 
       if (accesToken.Trim().Length == 0) 
       { 
        throw new Exception("no Access Token"); 
       } 

       return accesToken; 

      } 

      static void PostMessage(string accessToken, JobOfferModel message) 
      { 
       try 
       { 
        FacebookClient facebookClient = new FacebookClient(accessToken); 

        dynamic messagePost = new ExpandoObject(); 
        messagePost.access_token = accessToken; 

        messagePost.name = message.JobTitle; 
        messagePost.message = message.Content; 
        messagePost.caption = "www.Site.com"; 





        facebookClient.Post("me/feed", messagePost); 
       } 
       catch (FacebookOAuthException ex) 
       { 
        Console.WriteLine(ex); 
       } 
       catch (Exception ex) 
       { 
+0

你們在哪裏繼續複製你的代碼?自從YEARS以來,publish_stream已被棄用......無論如何,您需要使用頁面令牌。這個問題已經在stackoverflow上被多次詢問,只是搜索。 – luschn

+0

感謝您的回答。林有點新的這一點,所以我使用谷歌顯示我的最高結果:) – Qwerty

+0

總是先看看Facebook文檔,他們提供了示例代碼和所有你需要的信息。 – luschn

回答

0

正如luschn提到的,您需要使用頁面訪問令牌作爲頁面發佈。您可以通過向「我/帳戶」端點進行API調用來獲得此信息。這將返回您管理的頁面列表,以及每個頁面的訪問令牌。

使用頁面的訪問令牌,將下一個請求'me/feed'發佈爲該頁面。您可以在此處獲得有關使用頁面訪問令牌的更多詳細信息:https://developers.facebook.com/docs/facebook-login/access-tokens#pagetokens

您還應該更新您的登錄範圍,以使用新的「publish_actions」權限而不是「publish_stream」。

相關問題