0
 private void CheckAuthorization()
{
    string app_id = "*************";
    string app_secret = "*************";
    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);
        dynamic parameters = new ExpandoObject();
        parameters.message = "Test Posts";
        parameters.link = "http://blog.mysite.com";
        parameters.picture = "http://blog.mysite.com/images/logo.png";
        parameters.description = "Test Post";
        parameters.name = "Test Post";
        parameters.caption = "Welcome to test post";
        client.Post("/me/feed", parameters);
    }
}

嗨,上述方法是从 Facebook 获取访问令牌并发布在我的 Facebook 墙上。但如果 request[code] 为空,它会重定向到 Facebook 登录页面并询问用户名和密码。我的要求是有什么方法可以在不登录的情况下从 Facebook 获取访问令牌?或者在不重定向到 Facebook 登录页面的情况下登录的方式,例如静默或后台登录并通过请求 [代码] 获取访问令牌?

感谢您的支持......

4

0 回答 0