how to post to facebook page wall from .NET

前端 未结 7 800
执念已碎
执念已碎 2020-12-09 23:30

I\'ve created Facebook page. I have no application secret and no access token.

I want to post to this page from my .NET desktop application. How can I do it? Can any

相关标签:
7条回答
  • 2020-12-09 23:44

    You can use https://www.nuget.org/packages/Microsoft.Owin.Security.Facebook/ to obtain users login and permission and https://www.nuget.org/packages/Facebook.Client/ to post to feeds.

    Below example is for 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;
            }
        }
    

    On the callback you get user access token:

    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;
        // ...                   
    }
    

    Which then you can use to post to user's feed or page

    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 
                }
    
            }
    }
    

    I posted full example over here: https://klaatuveratanecto.com/facebook-wall-feed-posting-asp-net-mvc/

    0 讨论(0)
  • 2020-12-09 23:48
    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)
    {
    
    }
    }
    
    0 讨论(0)
  • 2020-12-09 23:52

    You will get information on how to create a facebook app or link your website to facebook on https://developers.facebook.com/?ref=pf.

    You will be able to download facebook sdk at http://facebooksdk.codeplex.com/. There are some good example given in the document section of the site.

    0 讨论(0)
  • 2020-12-09 23:55

    You need to grant the permission "publish_stream".

    0 讨论(0)
  • 2020-12-09 23:55

    Possibly the easiest way to do this is via Facebook PowerShell Module, http://facebookpsmodule.codeplex.com. This allows the same sort of operations as FacebookSDK, but via an IT-Admin scripting interface rather than a developer-oriented interface.

    AFAIK there is still a limitation of Facebook Graph API that you will not be able to post references to other pages (e.g. @Microsoft) using the Facebook Graph API. This will apply to FacebookSDK, FacebookPSModule, and anything else built over Facebook Graph API.

    0 讨论(0)
  • 2020-12-10 00:01

    You need to ask the user for the publish_stream permission. In order to do this you need to add publish_stream to the scope in the oAuth request you send to Facebook. The easiest way to do all of this is to use the facebooksdk for .net which you can grab from codeplex. There are some examples there of how to do this with a desktop app.

    Once you ask for that permission and the user grants it you will receive an access token which you can use to post to your page's wall. If you need to store this permission you can store the access token although you might need to ask for offline_access permission in your scope in order to have an access token that doesn't expire.

    0 讨论(0)
提交回复
热议问题