C# 在条件语句中使用response.redirect循环方法
我有一个功能,如果用户愿意,我可以代表他们发布到Facebook上。如果选中一个特定的复选框并单击一个按钮,它将调用一个在用户的墙上发布预设消息的方法。问题是,有一个重定向结束了该方法,但我需要它再运行一次,以获得实际发布到用户墙上的代码。任何帮助都将不胜感激 C#:C# 在条件语句中使用response.redirect循环方法,c#,asp.net,facebook,C#,Asp.net,Facebook,我有一个功能,如果用户愿意,我可以代表他们发布到Facebook上。如果选中一个特定的复选框并单击一个按钮,它将调用一个在用户的墙上发布预设消息的方法。问题是,有一个重定向结束了该方法,但我需要它再运行一次,以获得实际发布到用户墙上的代码。任何帮助都将不胜感激 C#: protectedvoid btnFbAuth_单击(对象发送方,事件参数e) { var fbAuth=(bool)会话[“fbAuth”]; 如果(fbAuth!=null) { 检查授权(); } } 私有无效检查授权()
protectedvoid btnFbAuth_单击(对象发送方,事件参数e)
{
var fbAuth=(bool)会话[“fbAuth”];
如果(fbAuth!=null)
{
检查授权();
}
}
私有无效检查授权()
{
字符串app_id=“myAppId”;
字符串app_secret=“myAppSecret”;
string scope=“发布\u流,管理\u页面”;
如果(请求[“代码”]==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));
}
其他的
{
字典标记=新字典();
字符串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,作用域,Request[“code”]。ToString(),app_secret);
HttpWebRequest-request=WebRequest.Create(url)为HttpWebRequest;
使用(HttpWebResponse=request.GetResponse()作为HttpWebResponse)
{
StreamReader=新的StreamReader(response.GetResponseStream());
字符串vals=reader.ReadToEnd();
foreach(vals.Split('&')中的字符串标记)
{
tokens.Add(token.Substring(0,token.IndexOf(“=”)),
子字符串(token.IndexOf(“=”)+1,token.Length-token.IndexOf(“=”-1));
}
}
字符串访问令牌=令牌[“访问令牌”];
var client=新的FacebookClient(访问令牌);
client.Post(“/me/feed”,new{message=“代表用户测试Facebook墙的帖子。”});
}
}
由于重定向后无法返回相同的POST请求,因此您需要将数据保存在服务器端的某个位置(即会话状态),并在开始处理请求时检查是否有从上一个请求到此页面的内容可以继续
注意,使用单独的页面来处理这种情况可能更容易——从FB重定向返回
protected void btnFbAuth_Click(object sender, EventArgs e)
{
var fbAuth = (bool)Session["fbAuth"];
if (fbAuth != null)
{
CheckAuthorization();
}
}
private void CheckAuthorization()
{
string app_id = "myAppId";
string app_secret = "myAppSecret";
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('&'))
{
tokens.Add(token.Substring(0, token.IndexOf("=")),
token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1));
}
}
string access_token = tokens["access_token"];
var client = new FacebookClient(access_token);
client.Post("/me/feed", new { message = "Testing a post on behalf of a user to Facebook wall." });
}
}