我正在使用Facebook C#SDK的最新SDK,即5.0.9 Beta
我在页面中有一个文本框按钮,假设在用户Wall中发布文本。
但问题是当我点击按钮时页面被重定向到
就像是
http://localhost:5000/facebookredirect.axd?code=JU1q_vSj13WRn1wIjHjCZRF5iDy_xvkFUppxADeS0F0.eyJpdiI6InJwejVVWXpJY1RqV0VaY1JjTl92ZGcifQ.KSa0B1ax1qCZZ-K_oXLmAZR8lyknWDRY9ieWxuLIZqXedUzb1WQH_FrcMF98VO6U1Dk5KIo4dz4AMdBxtfrUUH0ucgOoPC6_7Zb03WsIgY2fF84L-0s3A7m3f971sJUS4nQyRGDZ_-8oPuO0K0dTPg&state=eyJyIjoiaHR0cDovL2FwcHMuZmFjZWJvb2suY29tL3Rlc3RhcHBfdGVzdGluZy9Ib21lL0Fib3V0P0xlbmd0aD0zIn0
然后再次重定向到我的应用程序URL中间我没有在控制器中获取任何数据值显示为null。
在分析HTTP调用后,我发现内容是
<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>
我正在使用Ajax,但行为也是正常的。
它似乎像授权每次通话一样。 我是否需要实施Oauth 2.0。
我正在为MVC3使用示例APP。 谁能告诉我什么出错?
答案 0 :(得分:1)
当您使用FacebookAuthorizeAttribute
中的Facebook.Web.Mvc
时,这种情况很常见。至少,你的响应代码让我假设oauth握手不能正常工作(虽然你得到了“代码”,你忘了用它获得“access_token”)
如果您使用的是FacebookAuthorizeAttribute
,请检查您是否在facebook-developer-app中进行app-settings,并且您的web.config已插入正确的canvas-url / canvas-page。 (在您的情况下,为了测试,“http://localhost/”/“http://apps.facebook.com/yourappname”)
最近我读了一篇文章,说你不能使用Facebook.Mvc类,因为它们提供了示例代码。 见1. Answer on this link
答案 1 :(得分:0)
试试此代码
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);
}
回拨网址
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";
}
}
发布文字和图像代码
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;
}