简体   繁体   English

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

[英]Get AccessToken on the Landing Page using Facebook C# SDK

Coding Platform ASP.NET 4.0 WebForms 编码平台ASP.NET 4.0 WebForms

I have two pages that are relevant here 我有两页相关的页面

  1. Login.aspx 为Login.aspx
  2. LandingPage.aspx LandingPage.aspx

On Login.aspx when I click an ImageButton, I redirect to Facebook site with the following code 在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 "";
    }
}

That part is working properly. 那部分工作正常。 But I am clueless on how to access the user info at the LandingPage which is my redirect_uri. 但我对如何在LandingPage访问用户信息毫无头绪,这是我的redirect_uri。 Have tried this. 试过这个。

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

But I doubt it wont work since I dont have window.hash.location at Server Side(Its not working anyway) 但我怀疑它不会工作,因为我在服务器端没有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;

Although I am not sure, getting the Access Token will solve my problem, won't it? 虽然我不确定,获取Access Token会解决我的问题,不是吗? This is the error I am getting. 这是我得到的错误。

(OAuthException) An active access token must be used to query information about the current user. (OAuthException)必须使用活动访问令牌来查询有关当前用户的信息。

What am I doing wrong? 我究竟做错了什么?

here's a sample of a standalone website using webforms. 这是使用webforms的独立网站示例。 Check out Default.aspx.cs and Web.config . 查看Default.aspx.csWeb.config Please note that this sample could be modified for use with the latest source code and might not work with the latest release (5.0.3 beta). 请注意,此示例可能会被修改以与最新的源代码一起使用,并且可能无法与最新版本(5.0.3 beta)一起使用。

I just recently did this implementation. 我刚刚做了这个实现。 To access "me", you need to have an Access Token. 要访问“我”,您需要具有访问令牌。

The implementation is pretty straighforward, however, I will say that I found the major stumbling block for me was to make sure that my redirect_uri from my "Login.aspx" url matched the redirect_uri from my landing page. 实现非常简单,但是,我会说我发现我的主要绊脚石是确保我的“Login.aspx”网址中的redirect_uri与我的目标网页中的redirect_uri相匹配。

It must be simple, but I dodnt get it. 它必须简单,但我得到它。 So I used the lesser used Facebook C# SDK by Facebook . 所以我使用Facebook较少使用的Facebook C#SDK I pretty much did what this blog suggested . 我几乎完成了这个博客的建议 Everything is fine now. 现在一切都很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM