2011-03-08 101 views
4

我正在開發C#應用程序,它管理Facebook上使用Facebook C#SDK的粉絲頁面。我遇到了兩個問題,一個是在牆上發佈消息,另一個是在粉絲頁上創建事件。發佈在Facebook頁面作爲頁面不作爲管理員用戶使用Facebook C#SDK

是否可以在粉絲頁面牆上作爲粉絲頁面發佈消息而不是管理員用戶?

我可以編程方式使用Facebook C#SDK在粉絲頁面上創建事件(而不是作爲管理員,但作爲粉絲頁面)?

我經歷了其他一些SDK的其他教程,如Facebook PHP SDK。 PHP SDK允許將事件創建爲粉絲頁面,但對於C#SDK,創建事件不會給出任何結果。

+0

一個似乎有事件已被回答:) http://stackoverflow.com/questions/4925705/how-to-programmatically-add-an-event-to-a-page-using-graph-api/4926393#4926393 – VorTechS 2011-03-08 15:38:39

回答

1

要發佈消息,您需要授予manages_pages權限,並使用「/ me/accounts」的結果從Fan頁面的帳戶獲取訪問令牌。

下面是我用張貼消息本身的粉絲專頁:

      var dicParams = new Dictionary<string, object>(); 
         dicParams["message"] = stSmContentTitle; 
         dicParams["caption"] = string.Empty; 
         dicParams["description"] = string.Empty; 
         dicParams["name"] = smContent.CmeUrl; 
         dicParams["req_perms"] = "publish_stream"; 
         dicParams["scope"] = "publish_stream"; 

         // Get the access token of the posting user if we need to 
         if (destinationID != this.FacebookAccount.UserAccountId) 
         { 
          dicParams["access_token"] = this.getPostingUserAuthToken(destinationID); 
         } 
         publishResponse = this.FacebookConnection.Post("/" + destinationID + "/feed", dicParams); 
+0

如何你是否「授予每個管理頁面任務」? – camainc 2011-09-30 15:23:13

+0

@camainc - 首先確保您在Facebook上以您將用於牆上貼的身份登錄。然後在瀏覽器中使用此網址:https://www.facebook.com/dialog/oauth?client_id=[APPID]&redirect_uri=[YOUR網站網址]&scope = offline_access,publish_stream&response_type = token'。一旦您在FCBK對話框中允許,您將被重定向到您在上面添加了令牌的[[Your SITE URL]]部分中使用的URL(請參閱瀏覽器的地址欄)。請注意這裏的權限'offline_access',這將確保你的令牌不會過期。 – 2011-11-11 16:26:23

7

好吧,我只是碰到了這個同樣的事情。出於我的目的,我正在授權應用程序作爲粉絲頁面發佈到FB粉絲頁面。所以我可以讓多個用戶訪問該應用程序,但不能訪問粉絲頁面,以粉絲頁面的形式發佈。它符合我的要求。

無論如何,這裏是我如何使用C#Facebook SDK Beta V5.0.9。 第一步,確保您試圖發佈的用戶帳戶能夠發佈到粉絲頁面,並有權從應用程序中這樣做。

其中大部分類似於VorTechS的帖子,只是稍微澄清一下。

Dictionary<string,string> fbParams = new Dictionary<string,string>(); 
       fbParams["message"] = Title; 
       fbParams["caption"] = string.Empty; 
       fbParams["description"] = string.Empty; 
       fbParams["req_perms"] = "publish_stream"; 
       fbParams["scope"] = "publish_stream"; 
       //Initialize Your Facebook Client in the manner that suits you, I did it by supplying a saved access token from a single users 
       FacebookWebClient fbClient = new FacebookWebClient(<YOUR_ACCOUNT_ACCESS_TOKEN>); 
       //Get the listing of accounts associated with the user 
       dynamic fbAccounts = fbClient.Get("/me/accounts"); 

       //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 == <DESTINATION_ID_OF_YOUR_FAN_PAGE>) { 
         //When you find it, grab the associated access token and put it in the Dictionary to pass in the FB Post, then break out. 
         fbParams["access_token"] = account.access_token; 
         break; 
        } 
       } 
       //Then pass your destination ID and target along with FB Post info. You're Done. 
       dynamic publishedResponse = fbClient.Post("/" + <DESTINATION_ID_OF_YOUR_FAN_PAGE> + "/feed", fbParams); 
1

我想感謝您的回覆,這真的很有幫助。對我而言,它仍然在我的Facebook頁面上發佈爲ME(而不是頁面)。我認爲這只是我得到令牌的方式。雖然,foreach循環不適合我。所以,我這樣做:

public void postFacebook(object sender, EventArgs e) 
{ 
    //You will get your "myAccessToken" at this adress: 
    //https://www.facebook.com/dialog/oauth?client_id=<YOUR_APP_ID>&redirect_uri=<THE_PAGE_YOU_WILL_RECEIVE_YOUR_TOKEN_AT>&scope=offline_access,manage_pages,publish_stream&response_type=token 
    string myAccessToken = "<IN_THE_RETURNED_URL_BELOW>"; 
    string myPageId = "<YOUR_FAN_PAGE_ID>"; 

    Dictionary<string,string> fbParams = new Dictionary<string,string>(); 
    fbParams["message"] = "Testing 1 2 test"; 
    fbParams["caption"] = string.Empty; 
    fbParams["description"] = string.Empty; 
    fbParams["req_perms"] = "publish_stream"; 
    fbParams["scope"] = "publish_stream"; 
    //Initialize Your Facebook Client in the manner that suits you, I did it by supplying a saved access token from a single users 
    Facebook.FacebookAPI fbClient = new Facebook.FacebookAPI(myAccessToken); 
    //Get the listing of accounts associated with the user 
    var fbAccounts = fbClient.Get("/me/accounts"); 

    //Loop over the accounts looking for the ID that matches your destination ID (Fan Page ID) 
    foreach (var account in fbAccounts.Dictionary["data"].Array) 
    { 
     if (account.Dictionary["id"].String == myPageId) 
     { 
      //When you find it, grab the associated access token and put it in the Dictionary to pass in the FB Post, then break out. 
      fbParams["access_token"] = account.Dictionary["access_token"].String; 

      //Update the Access token (to post as the page). If you don't it will post as YOUR personal name. 
      fbClient.AccessToken = fbParams["access_token"]; 
      break; 
     } 
    } 



    //Then pass your destination ID and target along with FB Post info. You're Done. 
    dynamic publishedResponse = fbClient.Post("/" + myPageId + "/feed", fbParams); 
} 

順便問一下,你會需要這個SDK:SDK

1
在MVC4和C#SDK

,這讓我的目標

[FacebookAuthorize("publish_stream")] 
    public ActionResult PostNotification(FacebookContext context) 
    { 
     var dicParams = new Dictionary<string, object>(); 
     dicParams["message"] = "đang tập code cho facebook [email protected]@"; 
     dicParams["caption"] = "bbbbb"; 
     dicParams["description"] = "cccccc"; 
     dicParams["name"] = "http://www.facebook.com"; 
     dicParams["req_perms"] = "publish_stream"; 
     dicParams["scope"] = "publish_stream"; 
     string destinationID = context.UserId; 
     context.Client.Post("/" + destinationID + "/feed", dicParams); 
     return RedirectToAction("Index"); 
    } 
相關問題