在静态Web方法中使用Request和Response变量

时间:2013-02-12 04:29:26

标签: c# asp.net .net-4.0 facebook-c#-sdk webmethod

这是我的代码,

[WebMethod]
public static bool FirstAccess()
{
    string app_id = "139295982898884";
    string app_secret = "b5d2a88b56898610d54da2af498e3220";
    //string post_login_url = "http://dev.fitbook.com.asp1-23.ord1-1.websitetestlink.com/v4/FitbookContest.aspx";
    string post_login_url = "http://localhost:4327/FitbookContest.aspx";
    string scope = "publish_stream,manage_pages";

    string code = HttpContext.Current.Request["code"] ?? "";

    try
    {
        if (code == "")
        {
            string dialog_url = "http://www.facebook.com/dialog/oauth?" + "client_id=" + app_id + "&redirect_uri=" + HttpContext.Current.Server.UrlEncode(post_login_url) + "&scope=publish_stream";
            // HttpContext.Current.Response.Write(dialog_url);
            //HttpContext.Current.Response.Redirect(dialog_url, true);
            return false;
        }
        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, HttpContext.Current.Request.Url.AbsoluteUri, scope, HttpContext.Current.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));
                }
            }
            access_token = tokens["access_token"];
            TestFB.GetProfilePic(code);
            return true;
        }
    }
    catch (Exception ex)
    {
        return false;
    }
}

这里有像

这样的行
HttpContext.Current.Request["code"] ?? "";

HttpContext.Current.Response.Redirect(dialog_url, true);

无效。那么它有什么替代解决方案吗?有没有办法使用它?

1 个答案:

答案 0 :(得分:-1)

你试过这个吗,

string dialog_url = "~http://www.facebook.com/dialog/oauth?" + "client_id=" + app_id + "&redirect_uri=" + HttpContext.Current.Server.UrlEncode(post_login_url) + "&scope=publish_stream";

,重定向为false表示当前页面的执行是否应该终止

HttpContext.Current.Response.Redirect(dialog_url,false);

试试这个:

string code = HttpContext.Current.Request["code"];

if (code==null)
{
  // code
}

我觉得问题就在于此。您没有指定accesstoken

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, HttpContext.Current.Request.Url.AbsoluteUri, scope, HttpContext.Current.Request["code"].ToString(), app_secret);