Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Facebook C#SDK在登录页上获取AccessToken_C#_Asp.net_Facebook_Facebook C# Sdk - Fatal编程技术网

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

使用Facebook C#SDK在登录页上获取AccessToken,c#,asp.net,facebook,facebook-c#-sdk,C#,Asp.net,Facebook,Facebook C# Sdk,编码平台ASP.NET 4.0 WebForms 我这里有两页是相关的 Login.aspx LandingPage.aspx 在Login.aspx上,当我单击ImageButton时,我用以下代码重定向到Facebook站点 protected void FacebookLoginButton_Click(object sender, ImageClickEventArgs e) { try { Response.Redirect(GetFacebookLog

编码平台ASP.NET 4.0 WebForms

我这里有两页是相关的

  • Login.aspx
  • LandingPage.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 "";
        }
    }
    
    但我怀疑它不会工作,因为我在服务器端没有window.hash.location(它无论如何都不工作)

    虽然我不确定,但获得访问令牌将解决我的问题,不是吗? 这就是我得到的错误

    (OAutheException)活动访问 必须使用令牌进行查询 有关当前用户的信息


    我做错了什么?

    下面是一个使用webforms的独立网站示例。退房并离开。请注意,此示例可以修改以与一起使用,但可能无法与最新版本(5.0.3 beta版)一起使用。

    我最近刚刚完成了此实现。要访问“我”,您需要有一个访问令牌


    这个实现非常严格,但是,我要说的是,我发现我的主要障碍是确保我的“Login.aspx”url中的重定向uri与我的登录页中的重定向uri匹配。

    这一定很简单,但我不明白。所以我用了较少使用的。我非常喜欢。现在一切正常。

    +1实现了相同的代码。实际上,我的场景中有一个重定向uri。这个例子只有在winforms中才有。我正在尝试将其转换为webformsall。重定向非常有效。我对accesstoken和oAuth错误190有问题。谢谢你的帮助。你能给我看看你是怎么做到的吗?
        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;
            }
        }
    
        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;