6

我已創建Facebook頁面。 我沒有應用程序的祕密,也沒有訪問令牌。如何從.NET發佈到Facebook頁面牆

我想從我的.NET桌面應用程序發佈到此頁面。 我該怎麼辦?任何人都可以幫忙,我可以在哪裏獲得訪問令牌?

我應該創建一個新的Facebook應用程序嗎?如果是的話,我怎樣才能授予這個應用程序的權限在頁面的牆上發佈?

UPD1: 我沒有網站。 我需要將公司的新聞從.NET桌面應用程序發佈到公司的Facebook頁面。 我擁有的是Facebook頁面賬戶的登錄/密碼。

UPD2: 我創建了Facebook應用程序。使用AppID/SecretKey。我可以獲得訪問令牌。但是... 如何授予發佈到頁面牆的權限?

(OAuthException) (#200) The user hasn't authorized the application to perform this action

+0

您是否嘗試過*** Facebook SDK ***? – Kiquenet

回答

0

你會得到關於如何創建一個Facebook應用程序或鏈接你的網站在Facebook上https://developers.facebook.com/?ref=pf信息。

您將可以在http://facebooksdk.codeplex.com/下載facebook sdk。網站的文檔部分給出了一些很好的例子。

+0

好吧,我已經創建了新的Facebook應用程序。我如何授予這個應用程序的權限? – Lari13

+0

嘿檢查了這個https://developers.facebook.com/docs/reference/rest/comments.add/ Facebook有REST API添加評論。我希望這將是有用的在您的場景 –

+0

對不起,有什麼評論?我不需要它。我想從桌面應用程序發佈消息(帶圖片和說明)到頁面的牆上。 – Lari13

-1

可能最簡單的方法是通過Facebook PowerShell模塊http://facebookpsmodule.codeplex.com。這允許使用與FacebookSDK相同的操作,但通過IT管理員腳本界面而不是面向開發人員的界面。

AFAIK Facebook Graph API仍然存在限制,您將無法使用Facebook Graph API發佈對其他頁面(例如@Microsoft)的引用。這將適用於FacebookSDK,FacebookPSModule和其他基於Facebook Graph API的其他內容。

1

您需要授予「publish_stream」權限。

+0

如何設置權限「publish_stream」? – Kiquenet

2

您需要向用戶詢問publish_stream權限。爲了做到這一點,您需要將publish_stream添加到發送給Facebook的oAuth請求中的範圍。完成這一切的最簡單方法是使用可從codeplex中獲取的.net的facebooksdk。有一些例子說明如何用桌面應用程序來做到這一點。

一旦您詢問該權限並且用戶授予該權限,您將收到一個訪問令牌,您可以使用該令牌將帖子發佈到您的頁面牆上。如果您需要存儲此權限,則可以存儲訪問令牌,儘管您可能需要在範圍內請求offline_access權限才能使訪問令牌不會過期。

4

我創建展示瞭如何在這個位置做一個視頻教程:

http://www.markhagan.me/Samples/Grant-Access-And-Post-As-Facebook-User-ASPNet

你會發現,在我的例子,我問兩個「publish_stream」和「manage_pages」。這讓你也可以在用戶是管理員的頁面上發帖。下面是完整的代碼:

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Net; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

using Facebook; 

namespace FBO 
{ 
    public partial class facebooksync : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      CheckAuthorization(); 
     } 

     private void CheckAuthorization() 
     { 
      string app_id = "374961455917802"; 
      string app_secret = "9153b340ee604f7917fd57c7ab08b3fa"; 
      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 = 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"]; 

       var client = new FacebookClient(access_token); 

       client.Post("/me/feed", new { message = "markhagan.me video tutorial" }); 
      } 
     } 
    } 
} 
+0

這個請求在哪裏? – MonsterMMORPG

+0

你真的在上面的例子中寫了你正確的App_secret嗎?這些信息應該是絕密的 –

+0

如何根據用戶選擇發佈數據?就像在這個例子中,你在消息變量中發佈了一個靜態字符串,如果你要在CheckAuthorization()方法中使用該字符串,那麼在第一次重定向代碼之後如何訪問該字符串呢? –

0
public void PostImageOnPage() 
{ 
string filename=string.Empty; 
if(ModelState.IsValid) 
{ 
//-------- save image in image/ 
if (System.Web.HttpContext.Current.Request.Files.Count > 0) 
{ 
var file = System.Web.HttpContext.Current.Request.Files[0]; 
// fetching image      
filename = Path.GetFileName(file.FileName); 
filename = DateTime.Now.ToString("yyyyMMdd") + "_" + filename; 
file.SaveAs(Server.MapPath("~/images/Advertisement/") + filename); 
} 
} 
string Picture_Path = Server.MapPath("~/Images/" + "image3.jpg"); 
string message = "my message"; 
try 
{ 
string PageAccessToken = "EAACEdEose0cBAAoWM3X"; 

// ————————create the FacebookClient object 
FacebookClient facebookClient = new FacebookClient(PageAccessToken); 

// ————————set the parameters 
dynamic parameters = new ExpandoObject(); 
parameters.message = message; 
parameters.Subject = ""; 
parameters.source = new FacebookMediaObject 
{ 
ContentType = "image/jpeg", 
FileName = Path.GetFileName(Picture_Path) 
}.SetValue(System.IO.File.ReadAllBytes(Picture_Path)); 
// facebookClient.Post("/" + PageID + "/photos", parameters);// working for notification on user page 
facebookClient.Post("me/photos", parameters);// woring using bingoapp access token not page in(image album) Post the image/picture to User wall 
} 
catch (Exception ex) 
{ 

} 
} 
1

您可以使用 https://www.nuget.org/packages/Microsoft.Owin.Security.Facebook/獲取用戶的登錄和權限和 https://www.nuget.org/packages/Facebook.Client/ 張貼到飼料。

下面的例子是ASP.NET MVC 5:

public void ConfigureAuth(IAppBuilder app) 
     { 
      app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); 

      // Facebook 
      var facebookOptions = new FacebookAuthenticationOptions 
      { 
       AppId = "{get_it_from_dev_console}", 
       AppSecret = "{get_it_from_dev_console}", 
       BackchannelHttpHandler = new FacebookBackChannelHandler(), 
       UserInformationEndpoint = "https://graph.facebook.com/v2.4/me?fields=id,name,email,first_name,last_name,location", 
       Provider = new FacebookAuthenticationProvider 
       { 
        OnAuthenticated = context => 
        { 
         context.Identity.AddClaim(new Claim("FacebookAccessToken", context.AccessToken)); // user acces token needed for posting on the wall 
         return Task.FromResult(true); 
        } 
       } 
      }; 
      facebookOptions.Scope.Add("email"); 
      facebookOptions.Scope.Add("publish_actions"); // permission needed for posting on the wall 
      facebookOptions.Scope.Add("publish_pages"); // permission needed for posting on the page 
      app.UseFacebookAuthentication(facebookOptions); 

      AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.NameIdentifier; 
     } 
    } 

在回調你的用戶訪問令牌:

public ActionResult callback() 
{ 
    // Here we skip all the error handling and null checking 
    var auth = HttpContext.GetOwinContext().Authentication; 
    var loginInfo = auth.GetExternalLoginInfo(); 
    var identityInfo = auth.GetExternalIdentity(DefaultAuthenticationTypes.ExternalCookie); 

    var email = loginInfo.Email // [email protected] 
    var name = loginInfo.ExternalIdentity.Name // Klaatu Verata Necto 
    var provider = loginInfo.Login.LoginProvider // Facebook | Google 

    var fb_access_token = loginInfo.identityInfo.FindFirstValue("FacebookAccessToken"); 
    // Save this token to database, for the purpose of this example we will save it to Session. 
    Session['fb_access_token'] = fb_access_token; 
    // ...     
} 

然後你可以用它來發布到用戶的飼料或

public class postcontroller : basecontroller 
{      
     public ActionResult wall() 
     { 
      var client = new FacebookClient(Session['fb_access_token'] as string); 
      var args = new Dictionary<string, object>(); 
      args["message"] = "Klaatu Verata N......(caugh, caugh)"; 

      try 
      { 
       client.Post("/me/feed", args); // post to users wall (feed) 
       client.Post("/{page-id}/feed", args); // post to page feed 
      } 
      catch (Exception ex) 
      { 
       // Log if anything goes wrong 
      } 

     } 
} 

我在這裏發表了完整的例子: https://klaatuveratanecto.com/facebook-wall-feed-posting-asp-net-mvc/