Reputation: 2851
I'm attempting to make use of the Facebook C# SDK in my existing ASP.NET webforms application. My intent is to allow users to bypass the forms authentication by clicking the Facebook link. The URL on that link is as follows..
<a href="https://www.facebook.com/dialog/oauth?client_id=0000000000000&redirect_uri=http://localhost:2708/fboauth.aspx&scope=offline_access">facebook</a>
Facebook handles the request and then redirects back to fboauth.aspx which has the following code behind...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Facebook;
using System.Net;
using System.IO;
namespace StorageByMail20
{
public partial class fboauth : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string code;
code = Request.QueryString["code"];
Label1.Text = code;
string token;
string url = "https://graph.facebook.com/oauth/access_token?client_id=000000000000000&redirect_uri=http://localhost:2708/fboauth.aspx&client_secret=000000000000000000000000&code=" + code;
WebRequest request = WebRequest.Create(url);
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
token = reader.ReadToEnd();
string decodedtoken = HttpUtility.UrlDecode(token);
Facebook.FacebookAPI api = new Facebook.FacebookAPI(decodedtoken);
JSONObject result = api.Get("/first_name");
string name = result.Dictionary["first_name"].String;
Label2.Text = token;
Label3.Text = name;
}
}
}
What I'm attempting to do on this page is the following:
Everything works as intended up to the JSON call. If I comment out that line I'm OK. Otherwise Facebook returns the (400) Bad Request error. Can someone spot where I have gone wrong?
Note: there's a known bug reported here https://github.com/facebook/csharp-sdk/issues that relates to encoding issues with the access token. I think I've addressed that with the my use of UrlDecode. Also, please note that the SDK I'm attemtpting to use is the offical one (currently in Alpha) from Facebook. There are a few other C# SDKs that were created by the community.
Here are some particularly helpful articles I discovered while researching my issue.
onishimura.com/2010/08/11/facebook-c-and-asp-net-mvc-code-samples-for-friends-list-activities-list-and-wall-posts/
multitiered.wordpress.com/2010/08/05/getting-started-with-the-facebook-c-sharp-sdk/
*I removed "http://" from the links above. Evidently I need more reputation points before SO will allow me to include more than two links. Sorry folks!
Upvotes: 1
Views: 2846
Reputation: 1192
The SDK you link to on Github is not the Facebook C# SDK, it is a very old and very buggy commit that Facebook submitted and then left alone. There is a much more up to date and widely used project called the Facebook C# SDK here.
It seems that you are doing a lot of leg-work that could easily be handled by either the registration plugin or the javascript sdk (which is the convention when developing Facebook Connect sites.
If you are doing any more Facebook calls server-side I strongly recommend you use the linked C# SDK. Have a look at the sample to see how little coding you have to do.
Upvotes: 2