c# - 使用 Facebook C# SDK 在登陆页面上获取 AccessToken

标签 c# asp.net facebook facebook-c#-sdk

编码平台 ASP.NET 4.0 WebForms

我有两个相关的页面

  1. 登录.aspx
  2. 着陆页.aspx

在 Login.aspx 上,当我单击 ImageButton 时,我使用以下代码重定向到 Facebook 站点

protected void FacebookLoginButton_Click(object sender, ImageClickEventArgs e)
{
    try
    {
        Response.Redirect(GetFacebookLoginURL());
    }
    catch (System.Threading.ThreadAbortException)
    {
        throw;
    }
    catch (Exception err)
    {
        Elmah.ErrorSignal.FromCurrentContext().Raise(err);
    }
}

private string GetFacebookLoginURL()
{
    try
    {
        string baseURL = System.Configuration
            .ConfigurationManager
            .AppSettings["WebsiteURL"]
            .ToString();

        string[] extendedPermissions = new[] { 
            "publish_stream", 
            "offline_access" 
        };

        var oauth = new FacebookOAuthClient { 
            ClientId = FacebookContext.Current.AppId 
        };

        var parameters = new Dictionary<string, object>{
            { "response_type", "token" },
            { "display", "page" }
        };

        if (extendedPermissions != null && extendedPermissions.Length > 0)
        {
            var scope = new StringBuilder();
            scope.Append(string.Join(",", extendedPermissions));

            parameters["scope"] = scope.ToString();
        }
        parameters["redirect_uri"] = String.Format("{0}LandingPage.aspx", baseURL);
        return oauth.GetLoginUrl(parameters).OriginalString;
    }
    catch (Exception err)
    {
        Elmah.ErrorSignal.FromCurrentContext().Raise(err);
        return "";
    }
}

那部分工作正常。但是我对如何在我的 redirect_uri 的 LandingPage 上访问用户信息一无所知。已经试过了。

    FacebookOAuthClient cl = new FacebookOAuthClient(FacebookContext.Current);
    FacebookOAuthResult result = null; 
    string url = Request.Url.AbsoluteUri;

    // verify that there is a code in the url
    if (FacebookOAuthResult.TryParse(url, out result))
    {
        if (result.IsSuccess)                               
        {
            var accesstoken = result.AccessToken;
        }
        else
        {
            var errorDescription = result.ErrorDescription;
            var errorReason = result.ErrorReason;
        }
    }

但我怀疑它不会工作,因为我在服务器端没有 window.hash.location(无论如何它都不工作)

    var client = new FacebookClient(FacebookContext.Current);
    dynamic me = client.Get("me");
    string firstName = me.first_name;
    string lastName = me.last_name;
    string email = me.email;

虽然我不确定,但获取访问 token 会解决我的问题,不是吗? 这是我遇到的错误。

(OAuthException) An active access token must be used to query information about the current user.

我做错了什么?

最佳答案

这是一个使用网络表单的独立网站示例。查看Default.aspx.csWeb.config .请注意,可以修改此示例以用于 latest source code并且可能不适用于最新版本(5.0.3 测试版)。

关于c# - 使用 Facebook C# SDK 在登陆页面上获取 AccessToken,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5234592/

相关文章:

C# 使用 Nancy 获取 HTTP 正文

c# - 将 DataGridViewSelectedCellCollection 复制+粘贴到剪贴板或从剪贴板粘贴

.net - Web应用程序可在VS Development Server中运行,但不能在IIS中运行

asp.net - CustomErrors DefaultRedirect 到 Default.aspx 不起作用

facebook - 使用 js sdk 将 feed 发布到 pagewall

c# - 我应该为动态报告生成器使用哪种设计模式?

c# - URL 中的 "Greater than"和 "Lesser than"符号不起作用

c# - 内容类型 - WebAPI - 请求 header

javascript - 使用来自 Javascript 变量的消息更新 facebook 状态

facebook-unity-sdk 无法在 iOS 上运行 (facebook-unity-sdk)