如何在 C# 中从 Facebook SDK 获取当前用户访问令牌?不是应用程序访问令牌

How to get current user access token from Facebook SDK in C#? Not the App access token

下面的代码显示获取App Access Token,

public static string GetAppAccessToken()
    {
        var fb = new FacebookClient();

        dynamic result = fb.Get("oauth/access_token", new
        {
            client_id = "****************",
            client_secret = "*******************",
            grant_type = "client_credentials"
        });

        return result.access_token;
    }

但是如何使用 C# 获取使用 Facebook SDK 登录的当前用户的访问令牌?

    using System.Linq;
    using System.Net;
    using System.Web;
    using System.Web.Script.Serialization;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Facebook;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;    

        public class AccessUser
        {
            public string access_token { get; set; }
            public string token_type { get; set; }
            public string expires_in { get; set; }
        }

        private void CheckAuthorization()
            {
                string app_id = "**************";
                string app_secret = "************************";

                if (Request["code"] == null)
                {
                    var redirectUrl = string.Format("https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}", app_id, Request.Url.AbsoluteUri);
                    Response.Redirect(redirectUrl);
                }
                else
                {
                    AccessUser au = new AccessUser();

                    string url = string.Format("https://graph.facebook.com/v3.2/oauth/access_token?client_id={0}&redirect_uri={1}&client_secret={2}&code={3}", app_id, Request.Url.AbsoluteUri, app_secret, Request["code"].ToString());

                    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

                    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                    {
                        StreamReader reader = new StreamReader(response.GetResponseStream());
                        string vals = reader.ReadToEnd();
                        au = JsonConvert.DeserializeObject<AccessUser>(vals);
                        string token = au.access_token;

                    }
                }
            }

以上代码将 return 用户令牌。

注意: 使用 aspx 页面 构建,这是基于 V3.2 Graph API 调用。像1和2这样的版本,我们需要在URI中发送Scope

如果有人需要源代码,请告诉我...