2

这是我的代码,

[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);

不工作。那么有没有替代的解决方案呢?有什么方法可以使用它吗?

4

1 回答 1

-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";

并且,redirect as false 表示当前页面的执行是否应该终止

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

试试这个:

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

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

我认为问题就在于此。您没有指定访问令牌

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);
于 2013-02-12T05:11:01.600 回答