Creating your application in VS – Get Required Info
Now, writing some code. I was really waiting for that.
1. Open Visual Studio and create a new project. From the available projects types pick a Windows Forms Application. Ha? Windows Forms? Yes, Windows Forms. Facebook uses cookies in order to move from page to page during the process of logging in, and enabling you to get all the codes for submitting to the wall. Some of these cookies are being created only if you are a real browser. Of course, if you would like to make all of that in a Console, using HttpWebRequest and HttpWebResponse go ahead. Try. After you’ll sweat and fail, come back…
2. Welcome back. Trying to write a Console was really frustrating. Wasn’t it. But we are here to make things easy for you. OK. Drug into your form a WebBrowser control. Give it a name – like fbWebBrowser.
3. Change the ScriptErrorsSuppressed to true. That is to avoid any problems if there are script errors in Facebook pages. True means that all script errors will be ignored, so nothing will stop this Facebook show.
4. Set the Url property to: https://www.facebook.com/dialog/oauth?client_id=******* &redirect_uri=https://www.facebook.com/connect/login_success.html&scope= offline_access. Replace the “*” with the App Id that you copied from the first part of this tutorial (4).
5. Test the program – you should get in the browser a Facebook login page. Stop the program…
6. In the designer, double click on the WebBrowser control. That will open the code and create a method for the DocumentCompleted event. This event fires when the HTML page is fully charged in the browser. We will use this method in order to move throughout the various stages.
7. In order for our code to know exactly in what stage it is, declare an int variable outside the method but inside the class of the form. We will call that variable: reqCounter and set it to zero.
8. After the login page is loaded, we need to programmatically fill the textboxes (Email, Password) and click the button to move on. The textboxes are easy to find since they have a static ID attribute. The button ID is more difficult to find because it changes dynamically. Meaning that every time you load the first page, you’ll get a different ID for the button. So in order to find the button id, we will use a Regex – so you need to add to our code the namespace: System.Text.RegularExpressions. Let the code speak. This is how your code should look like as for now:
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 System.Text.RegularExpressions;
namespace FacebookAutoSubmitter
{
public partial class Form1 : Form
{
int reqCounter = 0;
public Form1()
{
InitializeComponent();
}
private void fbWebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
reqCounter++;
switch (reqCounter)
{
// Login – allow access
case 1:
fbWebBrowser.Document.GetElementById(“email”).SetAttribute(“value”, “YOUR@EMAIL”);
fbWebBrowser.Document.GetElementById(“pass”).SetAttribute(“value”, “PASSWORD”);
Regex _buttonID = new Regex(“(?<=uiButtonConfirm.*?for=\”).*?(?=\”)”);
string Html = fbWebBrowser.DocumentText;
string buttonID = _buttonID.Match(Html).Value;
fbWebBrowser.Document.GetElementById(buttonID).InvokeMember(“click”);
// After click – Get Code
break;
}
}
}
}
9. Try out this code – see that everything is fine. You should get a blank page with one word: Success – which means, that is correct – that you had a success. You had just verified the app by the user – which is you.
10. You think that you got just one word in stage 9, but you got more than that. The query string that came with the page has a nice secret, and that is a code. A code to let you use the app in your account. So we need to put our hands on that code and ask Facebook to get an access code for our app that we can use in order to write on the wall. Facebook wants to make sure, that the app that got a permission from the user – is a verified app. That is case 2 in our code:
case 2:
string code = fbWebBrowser.Url.Query.Replace(“?code=”, “”);
fbWebBrowser.Url = new System.Uri(“https://graph.facebook.com/oauth/access_token?”
+ “client_id=” + CLIENT_ID
+ “&client_secret=” + CLIENT_SECRET
+ “&code=” + code
+ “&redirect_uri=https://www.facebook.com/connect/login_success.html”
+ “&scope=manage_pages”);
// After redirecting – Access Code will be in the body of the HTML
break;
CLIENT_ID is the App ID and CLIENT_SECRET is the App Secret from stage 4 in part I.
11. Go ahead, test the code. You’ll get a white page, but this time – a long access code will be written in it.
12. And we need this access code. So first, declare a string outside the scope (inside the form class) so we would be able to take the access code outside this method and use it (in part III). We will call that string – Access_Token. Also – we will dispose our WebBrowser. Why? Because disposing a WebBrowser raises an event, and we will use that event in order to start writing on the wall. Only after completing this stage, we will be able to write on the wall, so we will use that event to know that we got there. So, this is case 3:
// Parse Acces Code
case 3:
Regex _token = new Regex(“(?<=access_token=).*?(?=<)”);
Access_Token = _token.Match(fbWebBrowser.DocumentText).Value;
fbWebBrowser.Dispose();
break;
13. In the next part, we will actually write to the Facebook wall, Stay tuned, you are almost done…