2015-04-06 104 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”。

相关问题