Reputation: 970
I am using the latest SDK of Facebook C# SDK i.e. 5.0.9 Beta
I had one Text box in page in button which suppose to post the text in the users Wall.
But the problem is when I am clicking on Button the page get redirected to
something like
http://localhost:5000/facebookredirect.axd?code=JU1q_vSj13WRn1wIjHjCZRF5iDy_xvkFUppxADeS0F0.eyJpdiI6InJwejVVWXpJY1RqV0VaY1JjTl92ZGcifQ.KSa0B1ax1qCZZ-K_oXLmAZR8lyknWDRY9ieWxuLIZqXedUzb1WQH_FrcMF98VO6U1Dk5KIo4dz4AMdBxtfrUUH0ucgOoPC6_7Zb03WsIgY2fF84L-0s3A7m3f971sJUS4nQyRGDZ_-8oPuO0K0dTPg&state=eyJyIjoiaHR0cDovL2FwcHMuZmFjZWJvb2suY29tL3Rlc3RhcHBfdGVzdGluZy9Ib21lL0Fib3V0P0xlbmd0aD0zIn0
and then again redirected to my apps URL in between I am not getting any data in my controller the values are showing as null.
After analyzing HTTP call I found the content is
<html><head><script type="text/javascript">
top.location = "http://www.facebook.com/dialog/oauth/?scope=user_about_me,friends_about_me,publish_stream&state=eyJyIjoiaHR0cDovL2FwcHMuZmFjZWJvb2suY29tL3Rlc3RhcHBfdGVzdGluZy9ob21lL0Fib3V0In0&client_id=218380811509677&redirect_uri=http://localhost:5000/facebookredirect.axd";
</script></head><body></body></html>
I am using Ajax but the Behavior is same for normal as well.
It Seems like Authorizing on every Call. Do I need to implement Oauth 2.0.
I am Using sample APP for the MVC3. Can anyone tell me whats going wrong?
Upvotes: 1
Views: 843
Reputation: 861
Try this code
public ActionResult Index()
{
var url = "http://www.facebook.com/v2.2/dialog/oauth/?scope=user_friends,read_friendlists,publish_actions,read_stream,read_insights,manage_pages,user_checkins,user_photos,read_mailbox,manage_notifications,read_page_mailboxes,email,user_videos,user_groups,offline_access,publish_actions,manage_pages&client_id=" + ConfigurationManager.AppSettings["ClientId"] + "&redirect_uri=" + ConfigurationManager.AppSettings["RedirectUrl"] + "&response_type=code";
return Redirect(url);
}
Call Back Url
public string AddFacebookAccount(string code)
{
string ret = string.Empty;
string client_id = ConfigurationManager.AppSettings["ClientId"];
string redirect_uri = ConfigurationManager.AppSettings["RedirectUrl"];
string client_secret = ConfigurationManager.AppSettings["ClientSecretKey"];
long friendscount = 0;
try
{
FacebookClient fb = new FacebookClient();
string profileId = string.Empty;
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("client_id", client_id);
parameters.Add("redirect_uri", redirect_uri);
parameters.Add("client_secret", client_secret);
parameters.Add("code", code);
JsonObject fbaccess_token = null;
try
{
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
fbaccess_token = (JsonObject)fb.Get("/oauth/access_token", parameters);
}
catch (Exception ex)
{
try
{
fbaccess_token = (JsonObject)fb.Get("/oauth/access_token", parameters);
}
catch (Exception ex1)
{
return "issue_access_token";
}
}
string accessToken = fbaccess_token["access_token"].ToString();
Session["AccessToken"] = accessToken;
if (accessToken != null)
{
fb.AccessToken = accessToken;
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
dynamic profile = fb.Get("v2.2/me");
dynamic friends = fb.Get("v2.2/me/friends");
try
{
Session["uid"] = profile.id;
friendscount = Convert.ToInt16(friends["summary"]["total_count"].ToString());
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
}
//return new JavaScriptSerializer().Serialize(ret);
return ret;
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
return "Something Went Wrong";
}
}
Post Text and Image Code
public ActionResult ComposeMessageSend(string message)
{
System.Net.ServicePointManager.Expect100Continue = false;
string file = Server.MapPath("~/Images/11.jpg");
message = "This is Photoshop's version of Lorem Ipsum. Proin gravida nibh vel velit auctor aliquet. Aenean sollicitudin, lorem quis bibendum auctor, nisi elit consequat ipsum, nec sagittis sem nibh id elit. Duis sed odio sit amet nibh vulputate cursus a sit amet mauris. Morbi accumsan ipsum velit. Nam nec tellus a odio tincidunt auctor a ornare odio. Sed non mauris vitae erat consequat auctor eu in elit. Class aptent" ;
string tokenid = string.Empty;
string userid = string.Empty;
//Arvind Itact
tokenid = "CAAK1OqZAcaoMBAHxFQXf78orU2KkZCijtr5MT14VBQsB9QB4YkL6Ua3FUHcSEpss7f0dwIPofpDI0oOSH94iaOQx9tbsS7zbZAu3To6R5dKo4jQ2HGXoiiiVBIEfEoVKwieOLzT6IvZAwlqMxK8x8gqR0RG9Dgd60NwCM3XRPDHZAeoUVYpSELoQdPJS1uDbNQFBK4mgtaSVPcbkjmD1VYhpC";
userid = "1383854058392012107";
string result = FacebookComposeMessage(tokenid, userid, message, file);
return View();
}
public string FacebookComposeMessage(string tokenid,string userid ,String message,string imagepath)
{
FacebookClient fb = new FacebookClient();
string ret = "";
fb.AccessToken = tokenid;
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls;
var args = new Dictionary<string, object>();
args["message"] = message;
if (!string.IsNullOrEmpty(imagepath))
{
var media = new FacebookMediaObject
{
FileName = "filename",
ContentType = "image/jpeg"
};
byte[] img = System.IO.File.ReadAllBytes(imagepath);
media.SetValue(img);
args["source"] = media;
ret = fb.Post("v2.0/" + userid + "/photos", args).ToString();
}
else
{
ret = fb.Post("v2.0/" + userid + "/feed", args).ToString();
// ret = fb.Post("/" + objFacebookAccount.FbUserId + "/photos", args).ToString();
// var data = fb.Get("v2.2" + ret);
}
return ret;
}
Upvotes: 0
Reputation: 442
That´s a typical occurrence , when you use FacebookAuthorizeAttribute
from Facebook.Web.Mvc
. At least, your response-code let me assume that the oauth-handshake didn´t work properly (although you got the "code", you forgot to get your "access_token" with it)
In case you´re using FacebookAuthorizeAttribute
, check if you´re app-settings in the facebook-developer-app AND your web.config have the right canvas-url/ canvas-page inserted. (In your case, for testing, "http://localhost/" / "http://apps.facebook.com/yourappname")
Recently I read a post, which said you mustn´t use the Facebook.Mvc classes, because they provide example-code. see 1. Answer on this link
Upvotes: 1