2013-08-30 57 views
0

因此,我有一個Facebook上的應用程序,它擁有publish_stream,manage_pages的權限,以及我迄今能夠將'文本'更新發布到我自己的個人資料中,顯示'通過應用程序名稱'。如何使用應用程序代表我的頁面將鏈接,圖片等發佈到我的Facebook頁面?

然後我試着從這個應用程序發佈到我的頁面上代表頁面。

我能做到這一點的client.Post("/page name/feed", new { message = "abcd" });(純文本),但如果我嘗試發佈鏈接,標題或圖片仍然張貼我的網頁上的,但我的個人資料像我分享該網頁上的東西,不會作爲我的頁面發佈。

我正在使用此方法發佈。

private void CheckAuthorization() 
    { 
     string app_id = "APP_ID"; 
     string app_secret = "APP_SECRET"; 
     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 = System.Net.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_tokens = tokens["access_token"]; 

      var client = new FacebookClient(access_tokens); 

      dynamic parameters = new ExpandoObject(); 

      parameters.message = "Check out this New Item"; 
      parameters.link = "https://www.example.com"; 
      parameters.picture = "http://www.example.com/images/img.jpg"; 
      parameters.name = "Item Title"; 
      parameters.caption = "Caption for the link"; 

      client.Post("/AllTheMed/feed", new { message = "lsjlsjlsjkldf" }); 

     } 
    } 
+0

請不要發佈您的應用程序的祕密,它應該是保守祕密。所以請重新生成它。另外,請嘗試使用頁面訪問令牌發佈並檢查您是否解決了您的問題。 –

+0

@Anvesh Saxena對不起,我很着急,是的,我已經嘗試使用頁面訪問令牌擴展令牌,但沒有好 –

回答

0

在您的代碼中,您只能獲得USER訪問令牌。但是,爲了發佈爲頁面,您需要獲取PAGE訪問令牌。在這裏看到更多的細節:https://developers.facebook.com/docs/authentication/pages/

在2個字,你需要做到以下幾點:

  1. 驗證用戶,並要求manage_pages許可
  2. 獲取的網頁列表中的用戶管理使用令牌從(1) - https://graph.facebook.com/me/accounts?access_token=USER_ACCESS_TOKEN
  3. 解析列表並獲取PAGE令牌 - 並將其用於發佈到提要。

基本上,在你

client.Post("/AllTheMed/feed", ...) 

你應該叫我/佔(這裏來僞代碼):

var myAccounts = client.Get("/me/accounts"); 
var pageAccessToken = myAccounts.Single(x => x.pageName == MY_PAGE_NAME).access_token; 

var pageClient = new FacebookClient(pageAccessToken); 
pageClient.Post("/me/feed", ...) 
相關問題