I’m trying to do a jQuery AJAX call to my MVC controller in my Facebook C# SDK Canvas application with something like:
$('#btnGetInfo').click(function () {
$.getJSON("/Home/GetInfo", null,
function (result) {
});
return false;
});
Controller:
public ActionResult GetInfo()
{
// do some Facebook call
var fb = new FacebookClient();
dynamic albums = fb.Get(string.Format("/{0}/photos", friendID));
}
I then get (OAuthException) An access token is required to request this resource , which I suppose is more or less expected. So I try to decorate the method:
[CanvasAuthorize(Permissions = "user_about_me,user_photos,.....)]
public ActionResult GetInfo()
{
...
}
But then the method is never called from the page.
With the sample project from CodePlex, putting the code in the Index() home controller works, so it’s not a configuration issue, not in the app nor in the Facebook settings.
I’m pretty sure this has something to do with persisting auth info thru post backs. Perhaps the CanvasAuthorizer class should be involved somehow, maybe the signed_request things? I’m new to FB programming and having a hard time putting the pieces together…
Update: If I call my method using a standard
<%: Html.ActionLink("DoThis", "GetInfo", "Home")%>
call, the method executes correctly (with a signed_request), so the question is now;
How can I do a jQuery call to my ASP.NET MVC controller?
The trick was to post the signed_request data in the ajax call (thanks ntotten):