(Facebook C# SDK) Problem, das eine access-token

Ich bin ziemlich neu auf dem Facebook C# SDK (5.0.3) die probablly ist der Grund für diese Frage.
Grundsätzlich, ich versuche, um die aktuelle Benutzer-Profil, E-Mail, Foto, etc etc. Unten finden Sie den code auf meine beiden Seiten (MyLogin.aspx und landingpage.aspx). Ich benutze web-Formulare kaufen die Weg.

Die erste Seite zeigt eine login-button, und leitet dann zu der landingpage. Siehe meine Kommentare im code weitere Informationen. Ich bekomme verschiedene Ausnahmen ich weiß nicht, wie zu lösen.

Wenn Sie keine Anleitung, die mir ermöglicht, vorwärts zu bewegen, bin ich sehr dankbar dafür.

So, hier ist der code ...

MyLogin.aspx.cs

    protected void DoLogin(object sender, EventArgs e)
    {
        Response.Redirect(GetFacebookLoginUrl());
    }


    private static string GetFacebookLoginUrl()
    {
        try
        {
            const string baseUrl = "http://localhost:5000/";
            var extendedPermissions = new[] { "offline_access", "publish_stream" };

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

            //If I use token instead of code the FacebookOAuthResult.TryParse will return false.
            var parameters = new Dictionary<string, object>{{ "response_type", "code" },{ "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);
            var url = oauth.GetLoginUrl(parameters).OriginalString;
            return url;
        }
        catch (Exception)
        {
            return string.Empty;
        }
    }

... und hier ist die Seite, wo alles scheint zu bekommen, um $"!@@%% ... wenn Sie wissen was ich meine.

Landingpage.aspx.cs

    protected void Page_Load(object sender, EventArgs e)
    {
        var cl = new FacebookOAuthClient(FacebookContext.Current);
        FacebookOAuthResult result = null;
        var url = Request.Url.AbsoluteUri;

        if (!FacebookOAuthResult.TryParse(url, out result)) return;
        if (result.IsSuccess)
        {
            //result.AccessToken is null, that's why I create a new instance of FacebookClient to get hold of the AccessToken.
            var accessToken1 = result.AccessToken;

            var app = new FacebookClient(FacebookContext.Current.AppId, FacebookContext.Current.AppSecret);
            var accessToken2 = app.AccessToken;

            //I now got an AccessToken but when I call client.Get("me");
            //I'll get (OAuthException) An active access token must be used to query information about the current user.

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

            //So, how do I get an active access token? 
            //Well, I did try using the FacebookOAuthClient object and the method ExchangeCodeForAccessToken (as you can see below).


            cl.ClientId = FacebookContext.Current.AppId;
            cl.ClientSecret = FacebookContext.Current.AppSecret;
            cl.RedirectUri = new UriBuilder("http://localhost:5000/").Uri;

            var parameters = new Dictionary<string, object> { { "permissions", "offline_access" } };
            var x = cl.ExchangeCodeForAccessToken(result.Code, parameters);

            //However, this now gives me the Exception (OAuthException) Error validating verification code.

        }
        else
        {
            var errorDescription = result.ErrorDescription;
            var errorReason = result.ErrorReason;
        }
    }

Vielen Dank!!

//Nicke

InformationsquelleAutor Nicke | 2011-03-11

Schreibe einen Kommentar