2016-06-25 31 views
0
private void CheckAuthorization() 
    { 
     string app_id = "*****"; 
     string app_secret = "******"; 
     string scope = "manage_pages,publish_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 = 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_token = tokens["access_token"]; 
      //tokens["access_token"]; 
      var client = new FacebookClient(access_token); 
      Dictionary<string, string> data = new Dictionary<string, string>(); 


      client.Post("/1038145769594318/feed", new 
      { 
       message = " test page post", 
       picture = "http://service.extrabucks.in/EmailTemplateCss/7/images/bottom-background2.png", 
       name = "Nikunj Patel", 
       link = "www.extrabucks.in", 
       description = "Test Description", 
       type = "links" 
      }); 
     } 
    } 

嘿我使用後在Facebook上使用C#這樣的代碼,但問題是,它沒有發佈的頁面是後一個用戶。我想在我的粉絲頁面中以頁面形式發佈。請使用上面的代碼給我一個解決方案。還有一件事我想要終身工作access_token我該怎麼辦? 由於事先後的Facebook頁面上使用圖形API使用C#作爲頁

回答

2
public static string GetPageAccessToken(string userAccessToken) 
    { 
     FacebookClient fbClient = new FacebookClient(); 
     fbClient.AppId = "*****"; 
     fbClient.AppSecret = "**************"; 
     fbClient.AccessToken = userAccessToken; 
     Dictionary<string, object> fbParams = new Dictionary<string, object>(); 
     JsonObject publishedResponse = fbClient.Get("/me/accounts", fbParams) as JsonObject; 
     JArray data = JArray.Parse(publishedResponse["data"].ToString()); 

     foreach (var account in data) 
     { 
      if (account["name"].ToString().ToLower().Equals("opening shortly")) 
      { 
       return account["access_token"].ToString(); 
      } 
     } 
     return String.Empty; 
    } 

此代碼每次

工作對我來說清爽頁面訪問令牌只需添加

client.Post("/1038145769594318/feed", new 
      { 
       message = " test page post", 
       picture = "", 
       name = "Nikunj Patel", 
       link = "www.extrabucks.in", 
       description = "Test Description", 
       type = "photo", 
       access_token = GetPageAccessToken(access_token) 
      }); 
相關問題