I covered this topic on my previous blog but then Facebook depreciated some stuff and the Facebook C# SDK changed so this is an update to that process.
I’ve tried to make this a full featured tutorial, so if you just want to get to the code, head down to the “Let the User Login To Facebook” section.
Get Your Facebook Key
Go to http://developers.facebook.com and log in with your Facebook account. Give them the permissions they need and go to the “Apps” tab. Click on “Create new App”
Fill in the App Display Name and the App Namespace and do the verification. Then you’ll get to a page that gives you your App ID and App Secret.
Don’t worry about anything else here… you don’t need to select anything in the “how your app integrates with Facebook” section to do this tutorial. If you’re using Facebook integration to do some basic login and simple posting, you could probably just use the Website option.
Also, click the “Graph API Explorer” on the left and keep that open. We’ll come back to that in a moment.
Add Facebook C# SDK
In Visual Studio go to Tools -> Library Package Manager -> Package Manager Console and type
Install-Package Facebook
And then
Install-Package Newtonsoft.Json
And *poof* you should have the Facebook C# SDK added to your project as well as a great JSON parser that will come in handy later.
Build Facebook Login UI
We’ll do this quick here (no MVVM, no bindings) but in later versions I’ll integrate this into a more formal project.
Open your project in Blend. For this quick-and-dirty tutorial, we’ll just add another page title and another panel for the UI we want to show when the user is logged in and set the Visibility to Collapsed on those. Our visual tree should look like this.
In our loginPanel, we’re going to add a button and a WebBrowser to our panel. Set the button (containing “sign in using facebook” in the content) to the top and the WebBrowser to fill the panel. For a little flare, I’m going to add “ShowFacebookLogin” and “HideFacebookLogin” animations. My login screen now looks like this (the WebBrowser will animate in when we press the button).
When we get our data back, we’re going to tell the user it worked by showing their name and their Facebook avatar. So we’ll add a Grid with an Image and a TextBlock to display the user, along with a friendly “Hello”. (Make sure to name all these things so that we can update them from the code.)
OK… out UI is simple, but ready to roll. Now let’s do something when the user clicks the sign-in button. Go to the “Click” event and add a method like “StartFacebookLogin”.
Let the User Login to Facebook
Add “using Facebook;” to your references at the top of your MainPage.xaml.cs file. Now, add the following properties:
private FacebookClient _asyncFbClient; private string _appID = "get from your facebook app page"; private string _appSecret = "get from your facebook app page";
Now go to your StartFacebookLogin method and add the following (explained at the end).
private void StartFacebookLogin(object sender, RoutedEventArgs e) { var uriElements = new Dictionary<string, object>(); uriElements.Add("client_id", _appID); uriElements.Add("redirect_uri", "https://www.facebook.com/connect/login_success.html"); uriElements.Add("response_type", "token"); var sb = new StringBuilder(); sb.Append("https://m.facebook.com/dialog/oauth?"); foreach(var kvp in dictionary) sb.AppendFormat("{0}={1}&", HttpUtility.UrlEncode(kvp.Key), HttpUtility.UrlEncode(kvp.Value.ToString())); sb.Length--; webBrowser.Navigated += new EventHandler<NavigationEventArgs>(CheckForAuth); webBrowser.Navigate(new Uri(sb.ToString())); }
This is a big change from the previous Facebook authentication process. Instead of adding our permissions, they are baked into our Facebook app. We just need to point to the mobile Facebook oauth page and let them know that we’d like to use the default redirect Uri.
At the point Facebook walks the user through their login (which should include identifying the device and giving the user info on what kind of sharing they can expect from our app) When they are done, it hands us back the access token we’ll need to get the user info and let the user make posts through our app.
This is why we attached the CheckForAuth event handler to our webBrowser. This is another big change from the previous version. We’ll be using our FacebookClient object
When we navigate to a new page, we’ll check to see if we got an access token using this code
private void CheckForAuth(object sender, System.Windows.Navigation.NavigationEventArgs e) { FacebookOAuthResult result; _asyncFbClient = new FacebookClient(); if (_asyncFbClient.TryParseOAuthCallbackUrl(returnUri, out result)) { if (result.IsSuccess) { IsolatedStorageSettings Settings = IsolatedStorageSettings.ApplicationSettings; if(Settings.Contains("MyFacebookAccessToken")) Settings["MyFacebookAccessToken"] = result.AccessToken; else Settings.Add("MyFacebookAccessToken", result.AccessToken); Settings.Save(); _asyncFbClient = new FacebookClient(result.AccessToken); _asyncFbClient.GetCompleted += new EventHandler<FacebookApiEventArgs>(_asyncFbClient_GetCompleted); _asyncFbClient.GetAsync("me"); } } }
If the Uri does contain a Facebook OAuth result and it is a success, we save the access token to our settings and then immediately use it to get the most basic user information.
The problem we have right now is that we have no class to structure the data that comes back. So get that all squared away.
First, right-click on “References” and select “Add Reference…”. Select “System.Runtime.Serialization”.
Next, add a class to your project (I added mine in a folder called “Models”) and name it FacebookUser.cs.
In that class, add the following code
[DataContractAttribute] public class FacebookUser { public FacebookUser() { } private string _id; [DataMember(Name = "id")] public string ID { get { return _id; } set { _id = value; } } private string _name; [DataMember(Name = "name")] public string Name { get { return _name; } set { _name = value; } } }
The sample project has a much larger (although still incomplete) version of this model. But this one will do for our purposes.
We’ll make our “GetCompleted” event handler (remember that?) look like this:
void _asyncFbClient_GetCompleted(object sender, FacebookApiEventArgs e) { FacebookUser _fbUser = JsonConvert.DeserializeObject<FacebookUser>(e.GetResultData().ToString()); Deployment.Current.Dispatcher.BeginInvoke(() => { fbName.Text = _fbUser.Name; BitmapImage bi = new BitmapImage(new Uri("https://graph.facebook.com/" + _fbUser.ID + "/picture")); fbAvatar.Source = bi; HideFacebookLogin.Begin(); }); _asyncFbClient.GetCompleted -= _asyncFbClient_GetCompleted; }
What we’ve done here is shove our data into a FacebookUser model. Then we use BeginInvoke to bring ourselves back to the UI thread and set all the properties we want.
Finally, we start the animation that hides the login data and shows that our Facebook login was a success.
Boom!