简体   繁体   中英

Facebook SDK: AccessToken.CurrentAccessToken always null, even after logging in

I am using the following code (on Xamarin) to log in using the latest Facebook SDK (I am also using Parse to manage my backend):

partial void LoginWithFacebook (UIButton sender)
        {
            LoginManager login = new LoginManager();
            login.LogInWithReadPermissionsAsync(kPermissions).ContinueWith(t => {
                if (t.IsFaulted && t.Exception != null) {
                    Console.Error.WriteLine("Error while authenticating: {0}", t.Exception.Message);
                } else {
                    var result = t.Result;
                    if (result.IsCancelled) {
                        Console.Error.WriteLine("User canceled the operation");
                    } else {
                        Console.WriteLine("Authenticated!");

                        ParseFacebookUtils.LogInAsync(result.Token.UserID, result.Token.TokenString, (DateTime)result.Token.ExpirationDate).ContinueWith(loginTask => {
                            if (!loginTask.IsFaulted) {
                                InvokeOnMainThread(() => PerformSegue("GoToDashboard", null));
                            } else {
                                Console.Error.WriteLine("Could not login to Parse");
                            }
                        });
                    }
                }
            });
        }

And then, to load the friends list (who are also using the App) I use the following code:

if (AccessToken.CurrentAccessToken != null) {
                var request = new GraphRequest ("me/friends", null);
                request.Start (new GraphRequestHandler ((connection, result, error) => {
                    if (error != null) {
                        Console.Error.WriteLine("Error fetching the friends list");
                    } else {
                        Console.WriteLine("Result: {0}", result);
                    }

                    hud.Hide(true);
                }));
            }

But the AccessToken always seem to be null even if the authentication is successful. I tried setting it by hand after the authentication but when the App restarts, it is lost again.

EDIT

I have removed the condition "if (AccessToken.CurrentAccessToken != null)" and it makes the request with no problems so I guess I am just using the wrong way to detect if the user is logged in. What's the correct way?

Thanks to the question referenced by @VijayMasiwal I was able to discover the problem. I had forgot to initialize Facebook in the App Delegate.

Here is how the FinishedLaunching method should be implemented when using Facebook:

public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
{
    Settings.AppID = kFacebookAppID;
    Settings.DisplayName = kFacebookDisplayName;

    // I had forgotten this line :)
    return ApplicationDelegate.SharedInstance.FinishedLaunching (application, launchOptions);
}

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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