c# - 在静态 web 方法中使用 Request 和 Response 变量

标签 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);

不工作。那么有没有其他解决方案呢?有什么方法可以使用吗?

最佳答案

你试过吗,

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
}

而且我认为问题出在线路上。您没有指定访问 token

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

关于c# - 在静态 web 方法中使用 Request 和 Response 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14825391/

相关文章:

c# - 初始化2个需要彼此作为参数的对象

c# - 无法添加引用 .NET

c# - 网络驱动器在启动时不可用

asp.net-mvc - ASP.net MVC3 View 继承

.net - 哪个版本的 .net 4.0 应该用于 Windows 服务(4.0 完整版或客户端配置文件)

c# - 访问文件中的备用数据流

c# - ASP.NET Core Startup.cs 中的 Kestrel 关闭功能

c# - asp.net:如何防止用户多次发布相同的数据

jquery - MVC3 jQuery 自动完成 - 用户必须选择选项,选项 ID 发送到服务器

asp.net - Paypal 订阅下一个到期日付款解决方案?