我为facebook墙贴设置了一个facebook应用,将其配置为指向我的localhost,并在facebook中创建了一个指向本地应用的标签。
一切正常,初始加载使我的页面正常,并且当我检查FacebookWebContext.Current.IsAuthenticated()
时,用户已经过身份验证。
我的问题是,当我发回数据时,Facebook上下文丢失,FacebookWebContext.Current.IsAuthenticated()
返回false。
我在.aspx页面检查过相同的代码;它运行良好,但同样的代码在mvc应用程序中不起作用。
我的代码
查看
<input value="Facebook Wall Posting" onclick="FacebookWall()" type="button" />
<script>
function fbAction()
{
var fbid=$('#txtFBID').val();
var url = "http://www.facebook.com/dialog/friends/?id=" + fbid + "&app_id=244816449015123&redirect_uri=http://localhost:15995/Saloon/CustomerPost";
location.href = url;
}
function FacebookWall() {
$.ajax({
type: "POST",
url: '@Url.Action("FacebookWall", "Saloon")',
contentType: "application/json; charset=utf-8",
//data: { FacebookId: $('#txtFBID').val() },
dataType: "json",
success: function() { }
});
}
</script>
控制器
[HttpPost]
public ActionResult FacebookWall()
{
string app_id = "34567777771276071";
string app_secret = "8466bc130961b988367f40f2234f34r4";
string scope = "publish_stream,manage_pages";
if (Request["code"] == null)
{
Response.Redirect(string.Format(
"https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}",
app_id, Request.Url.AbsoluteUri, scope));
}
else
{
Dictionary<string, string> tokens = new Dictionary<string, string>();
string url = string.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&scope={2}&code={3}&client_secret={4}",
app_id, Request.Url.AbsoluteUri, scope, Request["code"].ToString(), app_secret);
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
string vals = reader.ReadToEnd();
foreach (string token in vals.Split('&'))
{
//meh.aspx?token1=steve&token2=jake&...
tokens.Add(token.Substring(0, token.IndexOf("=")),
token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1));
}
}
string access_token = tokens["access_token"];
string fil = Server.MapPath("Images/2921-large-apple-icon-png-1.jp");
var client = new FacebookClient(access_token);
dynamic parameters = new ExpandoObject();
parameters.message = "Check out this funny article";
parameters.link = "http://www.natiska.com/article.html";
parameters.picture = "http://www.natiska.com/dav.png";
parameters.name = "Article Title";
parameters.caption = "Caption for the link";
client.Post("/me/feed", parameters);
}
return View();
}