[英]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): 我使用以下代码(在Xamarin上)使用最新的Facebook SDK登录(我也使用Parse管理后端):
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. 但是,即使身份验证成功,AccessToken似乎始终为空。 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? 我删除了条件“ if(AccessToken.CurrentAccessToken!= null)”,它使请求毫无问题,所以我想我只是使用错误的方式来检测用户是否已登录。正确的方法是什么?
Thanks to the question referenced by @VijayMasiwal I was able to discover the problem. 感谢@VijayMasiwal引用的问题,我得以发现问题。 I had forgot to initialize Facebook in the App Delegate.
我忘了在App Delegate中初始化Facebook。
Here is how the FinishedLaunching method should be implemented when using Facebook: 使用Facebook时,应如何实现FinishedLaunching方法:
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. 希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.