This is good to hear that we can create facebook windows application in c#, some time ago I check the facebook developers tab and found you can create facebook applications for different types of platform.
On here we are describing the process for developing facebook desktop application with the help of Facebook C# SDK, Facebook C# SDK is a community driven open source that enables developers to build Facebook applications across a variety platforms using any .NET language. With the help of Facebook C# SDK we can build web, desktop, Silverlight, and Windows Phone applications that integrate with Facebook. You can download this SDK from http://facebooksdk.codeplex.com/
First we need to create a facebook application in FaceBook:
1. Create a Facebook developer application from http://www.facebook.com/developers/.
2. After creating an application, Facebook provide you an App ID/API Key and an App Secret.
And now we can start project in C#
1. Start a new C# windows application and add one MDIparent windows form named MDIParent1.
2. Add a new windows form with name Form1 and put WebBrowser control on it.
Code in MDIparent1
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace FBApplication { public partial class MDIParent1 : Form { public MDIParent1() { InitializeComponent(); } private void MDIParent1_Load(object sender, EventArgs e) { label2.Text = "Connecting to facebook....."; Form1 frmLogin = new Form1(); frmLogin.MdiParent = this; frmLogin.Show(); } } }
Code in Form1
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Facebook; using System.Reflection; namespace FBApplication { public partial class Form1 : Form { private const string AppId = "Put your application id provided by facebook"; private readonly Uri _loginUrl; public FacebookOAuthResult _FacebookOAuthResult { get; private set; } private const string _ExtendedPermissions = "user_about_me,publish_stream,offline_access"; public Form1() { if (string.IsNullOrEmpty(AppId)) throw new ArgumentNullException("appId"); var _oauthClient = new FacebookOAuthClient { AppId = AppId }; IDictionary<string, object> _loginParameters = new Dictionary<string, object>(); _loginParameters["response_type"] = "token"; _loginParameters["display"] = "popup"; if (!string.IsNullOrEmpty(_ExtendedPermissions)) { _loginParameters["scope"] = _ExtendedPermissions; } _loginUrl = _oauthClient.GetLoginUrl(_loginParameters); InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { webBrowser1.Navigate(_loginUrl.AbsoluteUri); } private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e) { FacebookOAuthResult _oauthResult; if (FacebookOAuthResult.TryParse(e.Url, out _oauthResult)) { this._FacebookOAuthResult = _oauthResult; this.DialogResult = _FacebookOAuthResult.IsSuccess ? DialogResult.OK : DialogResult.No; try { var fb = new FacebookClient(_FacebookOAuthResult.AccessToken); var result = (IDictionary<string, object>)fb.Get("me"); var id = (string)result["id"]; string profilePictureUrl = string.Format("https://graph.facebook.com/{0}/picture?type={1}", id, "square"); MDIParent1 frmmdi; frmmdi = (MDIParent1)this.MdiParent; frmmdi.pictureBox1.LoadAsync(profilePictureUrl); try { Type tresult = result.GetType(); var objname = (string)result["name"]; var firstName = (string)result["first_name"]; var lastName = (string)result["last_name"]; frmmdi.textBox1.Text = id; frmmdi.textBox2.Text = firstName; frmmdi.textBox3.Text = lastName; frmmdi.label2.Text = "Successfully connected"; } catch (FacebookApiException ex) { MessageBox.Show(ex.Message); } } catch (FacebookApiException ex) { MessageBox.Show(ex.Message); } } else { this._FacebookOAuthResult = null; } } } }
thanks you
i can run visual studio but this show error FACEBOOK.
FB is missing
plz reply
I think you do not use the facebook c# sdk open source. you can download from here http://facebooksdk.codeplex.com/
if (FacebookOAuthResult.TryParse(e.Url, out _oauthResult))
dont work
“FacebookOAuthResult does not contain a definition for “TryParse”
i´m using Facebook 6.4.2
please help me
Same problem with me. Please tell the solution.
You sir are my hero
I’m getting the error in ‘Form’. What should I do?