VB.NET Facebook get access token for desktop application

facebook_logo

Following are the steps to Authenticate and Get Access Token for Facebook Users in your VB.NET, C# Facebook Application directly using the Facebook old Rest & Graph API rather then relaying on third party wrappers.

1) Embed a Web Browser control in your desktop application.

2) All the communication with Facebook is going to be through HTTP Request, Response method

3) Prepare your first Authentication request for token in the following manner:
Private _strAuthUrl As String = https://graph.facebook.com/oauth/authorize?client_id=117832888261979&redirect_uri=http://www.facebook.com/connect/login_success.html&scope=offline_access,publish_stream,user_status&type=user_agent

In this request you are asking Facebook to issue you an access token for this particular user by specifying the Client_Id which is your Application Id which you can get from your Facebook application developer page.

Further in this request you also specify the scope if you want your Application to have extended permissions from Facebook such as = offline_access which allows your app to connect to Facebook again and again without asking the users to login everytime.

4) Then you ask your Web Browser control to navigate to the above mentioned URL

WebBrowser1.Navigate(_strAuthUrl)

5) Once your Browser navigates to the above mentioned URL the Facebook Authentication page for your app opens in that browser asking User to enter his/her Id and Password. Once User hits the login button the Facebook issues the requested token against his/her Account and your App.

6) To catch this access token you need to listen the request at:

Private Sub WebBrowser1_Navigated ‘ this will be automatically fired around three times so you need to place your code under the if condition (mentioned below) to catch the token at the right time.

Then you need to put-in some logic to check that the URL you are getting as a response starts with something like below, this is important as there would be couple of more responses coming in as well before this so those needs to be discard.

If WebBrowser1.Url.ToString.StartsWith(“http://www.facebook.com/connect/login_success.html#access_token=”) Then
Put in your logic here to get the browser URL as a string and extract access token through string manipulation functions like substring and split.
           dim url as string
           dim arr as string() 
           url = WebBrowser1.Url.ToString ‘’ convert the url into string
           url = url.Substring(64) ‘ the position from where you access token starts
           arr = url.Split(“&”) ‘’now you split the url on basis of “&” delimeter and
the token comes on arr(0) location
End If

The above mentioned logic depends from developer to developer but the main workflow would remain the same.