Home > Uncategorized > Automated access_token grant using Facebook and C=

Automated access_token grant using Facebook and C=

Say you wanted to get an access token for facebook from a desktop app, but don’t want to display the normal facebook login screen. – and you somehow know the user’s facebook username and password  – then this approach might help you get the access token you need;

first, you need to set up a facebook app, and get the client_id

public void GetAccessToken(Action<string,string> Callback)
{
this.Callback = Callback;
string strUrl = “https://www.facebook.com/dialog/oauth?&#8221;;
strUrl += “type=user_agent&”;
strUrl += “client_id=xxxx&”;
strUrl += “redirect_uri=xxxxx”;
this.webBrowser1.DocumentCompleted += WebBrowser1OnDocumentCompleted;
this.webBrowser1.Navigate(strUrl);
}

Here, I’m assuming you have a windows form, with a web browser control called webBrowser1 – redirect_uri has to be whatever was configured in the facebook app, but other than that, it doesn’t matter.

then we fill out the username / password and click the login burron on Document completed;

private void WebBrowser1OnDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs webBrowserDocumentCompletedEventArgs)
{
if (webBrowser1.Url.Host == “<domain>”)
{
var variables = HttpUtility.ParseQueryString(webBrowser1.Url.Fragment);
var access_token = variables[“#access_token”];
var expires_in = variables[“expires_in”];
Callback(access_token, expires_in);
}
else
{
var strJs1 = @”document.getElementsByName(’email’)[0].value ='” + strUsername + “‘”;
var strJs2 = @”document.getElementsByName(‘pass’)[0].value = ‘” + strPassword + “‘”;
var strJs3 = @”document.getElementsByName(‘login’)[0].click()”;
SendJs(strJs1);
SendJs(strJs2);
SendJs(strJs3);
}
}

SendJS is defined as follows;

protected string SendJs(string jScript)
{
object[] args = { jScript };
var strReturn = webBrowser1.Document == null ? “” : webBrowser1.Document.InvokeScript(“eval”, args);
System.Diagnostics.Debug.WriteLine(jScript);
System.Diagnostics.Debug.WriteLine(strReturn);
return strReturn == null ? “” : strReturn.ToString();
}

You’ll also need the following global variables

public string strUsername = “<email>”;
public string strPassword = “<password>”;

public Action<string, string> Callback;

Hope this helps someone out!

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: