如何从.NET发布到Facebook页面墙

我创建了Facebook页面。 我没有应用程序密钥,也没有访问令牌。

我想从我的.NET桌面应用程序发布到此页面。 我该怎么做? 任何人都可以帮忙,我在哪里可以获得访问令牌?

我应该创建一个新的Facebook应用程序吗? 如果是,我如何授予此应用程序的权限以在页面的墙上发布?

UPD1:我没有网站。 我需要将公司的新闻从.NET桌面应用程序发布到公司的Facebook页面。 我所拥有的只是Facebook页面帐户的登录/密码。

UPD2:我创建了Facebook应用程序。 使用AppID / SecretKey。 我可以获得访问令牌。 但是……我如何授予发布到页面墙的权限?

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

我已经创建了一个video教程,展示了如何在这个位置执行此操作:

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 tokens = new Dictionary(); 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" }); } } } } 

您需要向用户询问publish_stream权限。 为此,您需要将publish_stream添加到您发送给Facebook的oAuth请求中的范围。 最简单的方法是使用facebooksdk for .net,你可以从codeplex中获取。 有一些关于如何使用桌面应用程序执行此操作的示例。

一旦您要求获得该许可并且用户授予该许可,您将收到一个访问令牌,您可以使用该令牌发布到您的页面墙上。 如果您需要存储此权限,则可以存储访问令牌,尽管您可能需要在范围中请求offline_access权限才能拥有不会过期的访问令牌。

您需要授予权限“publish_stream”。

您可以使用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 // klaatuveratanecto@gmail.com 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; // ... } 

然后您可以使用它发布到用户的Feed或页面

 public class postcontroller : basecontroller { public ActionResult wall() { var client = new FacebookClient( Session['fb_access_token'] as string); var args = new Dictionary(); 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/

您将获得有关如何创建Facebook应用程序或将您的网站链接到Facebook的信息, url为https://developers.facebook.com/?ref=pf 。

您可以在http://sofzh.miximages.com/c%23/ + 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) { } }

可能最简单的方法是通过Facebook PowerShell Module, http://facebookpsmodule.codeplex.com 。 这允许与FacebookSDK相同的操作,但是通过IT-Admin脚本界面而不是面向开发人员的界面。

AFAIK仍然存在Facebook Graph API的限制,您将无法使用Facebook Graph API发布对其他页面(例如@Microsoft)的引用。 这将适用于FacebookSDK,FacebookPSModule以及通过Facebook Graph API构建的任何其他内容。