Facebook C# API: Getting the access token

Fri. July 23, 2010
Categories: C#
Tags: ,
// From the Facebook C# API page:
// "Most data accessible via the Graph API required an access token.
//  This SDK does not include a method of getting a token from a user."
// http://github.com/facebook/csharp-sdk
 
// The following code is for a dialog form named frmLogin. When the dialog
// returns with a DialogResult of OK, the access token will be in the form's
// Tag property.
 
private void frmLogin_Load(object sender, EventArgs e)
{
    string application_id = "123456789012345";
 
    browserLogin.Navigate("https://graph.facebook.com/oauth/authorize" +
        "?client_id=" + application_id +
        "&redirect_uri=http://www.facebook.com/connect/login_success.html" +
        "&type=user_agent" +
        "&display=popup"
    );
}
 
private void browserLogin_DocumentCompleted(object sender,
    WebBrowserDocumentCompletedEventArgs e)
{
    if (browserLogin.Document.Url.ToString().Contains("access_token"))
    {
        string token = String.Empty;
        string URL = browserLogin.Document.Url.ToString();
 
        token = URL.Substring(URL.IndexOf("access_token") + 13);
        token = token.Substring(0, token.IndexOf("&"));
 
        // Received access token
        this.Tag = token;
        this.DialogResult = DialogResult.OK;
    }
}