Get FaceSharp with NuGet
Development Guide Page 1 of 4- Get FaceSharp with NuGet
- Apply Attributes on Controllers for authentication.
- Initialize Facebook JavaScript SDK Properly Learn how to initialize the Javascript SDK AND Facesharp Server Side at the same time.
- Make Facebook API Calls with FaceSharp
Getting Up in Runing with FaceSharp is very simple
Just follow the steps below and you'll be ready to start coding- Create a new .NET MVC Project (3/4) or open an existing project.
- Add FaceSharp through NuGet - http://nuget.org/List/Packages/FaceSharp
- Add the FacebookCore.cs file to your project (see below for sample)
- Add entry into Web.Config for Castle
- Add Attributes to your controllers
- Enjoy Facebook development with FaceSharp:
FacebookCore.cs (created in Code/Facebook)
Be sure to replace <<your assembly>> with your web project's assembly name.using System; using System.Configuration; using System.Web; using FaceSharp.Api; using FaceSharp.Api.Types; namespace <<your assembly>>.Code.Facebook { public class FacebookCore : IFacebookCore { private readonly string _applicationId; private readonly string _applicationSecret; private readonly string _applicationUrl; private readonly string _extendedPermissions; public string ApplicationId { get { return _applicationId; } } public string ApplicationSecret { get { return _applicationSecret; } } public string ApplicationUrl { get { return _applicationUrl; } } public string ExtendedPermissions { get { return _extendedPermissions; } } public FacebookCore() { _applicationId = ConfigurationManager.AppSettings["FacebookApplicationId"]; _applicationSecret = ConfigurationManager.AppSettings["FacebookApplicationSecret"]; _applicationUrl = ConfigurationManager.AppSettings["FacebookApplicationUrl"]; _extendedPermissions = ConfigurationManager.AppSettings["FacebookExtendedPermissions"]; if (_applicationId != null && HttpContext.Current.Items["ApplicationId"] == null) HttpContext.Current.Items.Add("ApplicationId", _applicationId); if (_applicationSecret != null && HttpContext.Current.Items["ApplicationSecret"] == null) HttpContext.Current.Items.Add("ApplicationSecret", _applicationSecret); if (_applicationUrl != null && HttpContext.Current.Items["ApplicationUrl"] == null) HttpContext.Current.Items.Add("ApplicationUrl", _applicationUrl); if (_extendedPermissions != null && HttpContext.Current.Items["ExtendedPermissions"] == null) HttpContext.Current.Items.Add("ExtendedPermissions", _extendedPermissions); } public FaceSharp.Api.Types.User CurrentUser { get { var user = HttpContext.Current.Items["current_user"]; if (user == null) return null; return (FaceSharp.Api.Types.User)user; } set { if (HttpContext.Current.Items["current_user"] == null) { HttpContext.Current.Items.Add("current_user", value); } else { HttpContext.Current.Items["current_user"] = value; } } } public string Code { get { var code = HttpContext.Current.Items["code"]; return code == null ? null : code.ToString(); } set { if (HttpContext.Current.Items["code"] == null) { HttpContext.Current.Items.Add("code", value); } else { HttpContext.Current.Items["code"] = value; } } } public string AccessToken { get { return HttpContext.Current.Items["access_token"] == null ? "" : HttpContext.Current.Items["access_token"].ToString(); } set { if (HttpContext.Current.Items["access_token"] == null) { HttpContext.Current.Items.Add("access_token", value); } else { HttpContext.Current.Items["access_token"] = value; } } } public DateTime? AccessTokenExpirationDate { get { var accessTokenExpirationDate = HttpContext.Current.Items["access_token_expiration_date"]; if (accessTokenExpirationDate == null) return null; return (DateTime?)accessTokenExpirationDate; } set { if (HttpContext.Current.Items["access_token_expiration_date"] == null) { HttpContext.Current.Items.Add("access_token_expiration_date", value); } else { HttpContext.Current.Items["access_token_expiration_date"] = value; } } } public SignedRequest SignedRequest { get { var signedRequest = HttpContext.Current.Items["signed_request"]; if (signedRequest == null) return null; return (FaceSharp.Api.Types.SignedRequest)signedRequest; } set { if (HttpContext.Current.Items["signed_request"] == null) { HttpContext.Current.Items.Add("signed_request", value); } else { HttpContext.Current.Items["signed_request"] = value; } } } } }
Web.config Settings
Be sure to replace <<your assembly>> with your web project's assembly name.Be sure to replace the Facebook Settings from the Facebook App you created.
<configSections> ... <section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" /> ... </configSections> <castle> <components> <component id="FacebookCore" service="FaceSharp.Api.IFacebookCore,FaceSharp" type="<<your assembly>>.Code.Facebook.FacebookCore, <<your assembly>>" /> </components> </castle> <appSettings> .... <add key="FacebookApplicationId" value="<<Your Facebook App Id>>" /> <add key="FacebookApplicationSecret" value="<<Your Facebook App Secret>>" /> <add key="FacebookApplicationUrl" value="<<Your Facebook App Url>>" /> <add key="FacebookExtendedPermissions" value="<<Any Extended Permissions You Need>>" /> .... </appSettings>