ios facebook sdk 4.0 登录错误代码 304
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29408299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ios facebook sdk 4.0 login error code 304
提问by jim
I've just updated facebook sdk v4.0
我刚刚更新了 facebook sdk v4.0
and according the tutorial of Using Custom Login UIs
并根据使用自定义登录用户界面的教程
-(IBAction)facebookLoginClick:(id)sender {
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
// Process error
} else if (result.isCancelled) {
// Handle cancellations
} else {
// If you ask for multiple permissions at once, you
// should check if specific permissions missing
if ([result.grantedPermissions containsObject:@"email"]) {
// Do work
}
}
}];
}
BUT the result is always nil and error code is 304, am I missing something?
但结果总是 nil 并且错误代码是 304,我错过了什么吗?
回答by Vineeth Joseph
I had a similar problem.
我有一个类似的问题。
After initialising FBSDKLoginManager
I added a line to flush out the data and the (Facebook)Token:
初始化后,FBSDKLoginManager
我添加了一行来清除数据和 (Facebook)Token:
FBSDKLoginManager *loginmanager= [[FBSDKLoginManager alloc]init];
[loginmanager logOut];
Hope this helps.
希望这可以帮助。
Thus, exactly as the OP asks, "am I missing something"?
因此,正如 OP 所问的那样,“我是否遗漏了什么”?
Yes, the following standard example code which is seen everywhere, is simply wrong:
是的,以下随处可见的标准示例代码完全是错误的:
-(IBAction)facebookLoginClick:(id)sender
{
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
---- ONE MAGIC LINE OF CODE IS MISSING HERE ----
[login logInWithReadPermissions:@[@"email"]
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
{
if (error) {...}
else if (result.isCancelled) {...}
else { // (NB for multiple permissions, check every one)
if ([result.grantedPermissions containsObject:@"email"])
{ NSLog(@"%@",result.token); }
}
}];
}
you must do this:
你必须这样做:
-(IBAction)facebookLoginClick:(id)sender
{
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logOut]; //ESSENTIAL LINE OF CODE
[login logInWithReadPermissions:@[@"email"]
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
{
if (error) {...}
else if (result.isCancelled) {...}
else { // (NB for multiple permissions, check every one)
if ([result.grantedPermissions containsObject:@"email"])
{ NSLog(@"%@",result.token); }
}
}];
}
Otherwise, very simply, the app will not workif the user happens to change FB accountson the device. (Unless they happen to for some reason re-install the app!)
否则,很简单,如果用户碰巧在设备上更改了 FB 帐户,该应用程序将无法运行。(除非他们碰巧出于某种原因重新安装该应用程序!)
Once again - the popular sample code above simply does not work(the app goes in to an endless loop) if a user happens to change FB accounts. The logOut
call must be made.
再一次 -如果用户碰巧更改了 FB 帐户,上面的流行示例代码根本不起作用(应用程序进入无限循环)。该logOut
调用必须进行。
回答by amosel
This happened to me when I changed the Facebook AppID (in the Info.plist file) switching form a test Facebook app to a production Facebook app. If your app sends the Facebook token to a server that generate a JWT for for instance, Facebook SDK still persists the fbToken (FBSDKAccessToken.currentAccessToken()
) persisted unless it's told to remove it.
That may also happen in production when a user logs out of the app, a log out API request will log the user out and reset your JWT. But even before sending the log out API request, the client will need to tell the FBSDKLoginManager
instance to log out.
当我更改 Facebook AppID(在 Info.plist 文件中)从测试 Facebook 应用程序切换到生产 Facebook 应用程序时,这发生在我身上。例如,如果您的应用程序将 Facebook 令牌发送到生成 JWT 的服务器,Facebook SDK 仍会保留 fbToken ( FBSDKAccessToken.currentAccessToken()
) ,除非它被告知将其删除。当用户注销应用程序时,这也可能发生在生产环境中,注销 API 请求将使用户注销并重置您的 JWT。但即使在发送注销 API 请求之前,客户端也需要告诉FBSDKLoginManager
实例注销。
if let currentAccessToken = FBSDKAccessToken.currentAccessToken() where currentAccessToken.appID != FBSDKSettings.appID()
{
loginManager.logOut()
}
回答by Adrian999
I had this, it occurred when changing the FB app details in Xcode while an iOS app was running. You need to delete the app from the device and then republish with the new FB app setup. Just recompiling is not enough to clear the old FB settings
我有这个,它发生在 iOS 应用程序运行时更改 Xcode 中的 FB 应用程序详细信息时。您需要从设备中删除该应用程序,然后使用新的 FB 应用程序设置重新发布。仅重新编译不足以清除旧的 FB 设置
回答by Abhishek Jain
Swift 3.0
斯威夫特 3.0
func loginWithFacebook
{
let loginManager = FBSDKLoginManager()
if let currentAccessToken = FBSDKAccessToken.current(), currentAccessToken.appID != FBSDKSettings.appID()
{
loginManager.logOut()
}
loginManager.logIn(withReadPermissions: ["public_profile","email"], from: self, handler: { (result, error) in
if error != nil {
print("error\(String(describing: error))")
}
else if (result?.isCancelled)! {
}
else {
print(FBSDKAccessToken.current())
}
})
}
回答by Piyush
You can use the following code to solve your problem :
您可以使用以下代码来解决您的问题:
[FBSDKAccessToken refreshCurrentAccessToken:^(FBSDKGraphRequestConnection *connection, id result, NSError *error){}
回答by Joseph Lin
According to the docs, 304 FBSDKLoginUserMismatchErrorCode
"Indicates a failure to request new permissions because the user has changed".
根据文档,304 FBSDKLoginUserMismatchErrorCode
“表示由于用户已更改而无法请求新权限”。
Make sense in the scenario @Adrian999 mentioned.
在提到的场景@Adrian999 中有意义。
回答by Tanjima Kothiya
swift 4.0, 4.2, 5.0
快速 4.0、4.2、5.0
FBSDKLoginManager().logOut()
回答by Maxime Felici
Swift 4.0and FBSDKCoreKit 4.33
Swift 4.0和FBSDKCoreKit 4.33
let loginManager = LoginManager()
if let currentAccessToken = AccessToken.current, currentAccessToken.appId != SDKSettings.appId
{
loginManager.logOut()
}
回答by iCrany
Actually, the reason is very simple, you can try following step to reappear this error code easily.
其实原因很简单,您可以尝试按照以下步骤轻松重现此错误代码。
The "Facebook login fail" may have two reasons:
“ Facebook登录失败”可能有两个原因:
- you get the user info from facebook fail.
- you get the user info from facebook success,but upload to your own server fail.
- 您从 facebook 获取用户信息失败。
- 您从 facebook 成功获取用户信息,但上传到您自己的服务器失败。
The code in FBSDKLoginManager.m
is:
中的代码FBSDKLoginManager.m
是:
- (void)validateReauthentication:(FBSDKAccessToken *)currentToken withResult:(FBSDKLoginManagerLoginResult *)loginResult
{
FBSDKGraphRequest *requestMe = [[FBSDKGraphRequest alloc] initWithGraphPath:@"me"
parameters:nil
tokenString:loginResult.token.tokenString
HTTPMethod:nil
flags:FBSDKGraphRequestFlagDoNotInvalidateTokenOnError | FBSDKGraphRequestFlagDisableErrorRecovery];
[requestMe startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
NSString *actualID = result[@"id"];
if ([currentToken.userID isEqualToString:actualID]) {
[FBSDKAccessToken setCurrentAccessToken:loginResult.token];
[self invokeHandler:loginResult error:nil];
} else {
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
[FBSDKInternalUtility dictionary:userInfo setObject:error forKey:NSUnderlyingErrorKey];
NSError *resultError = [NSError errorWithDomain:FBSDKLoginErrorDomain
code:FBSDKLoginUserMismatchErrorCode
userInfo:userInfo];
[self invokeHandler:nil error:resultError];
}
}];
}
when currentToken.userID
is not equal actualID
, the FBSDKLoginUserMismatchErrorCode
will throw.
当currentToken.userID
不相等时actualID
,FBSDKLoginUserMismatchErrorCode
将抛出。
Now, this issue will reappear when user A login facebook fail,but you do not have [FacebookSDKManager logOut]
after the fail, the app will cache the accessToken for the user A , and then user A change facebook account to user B, when user B login facebook again,it will reappear this issue.
现在,当用户 A 登录 facebook 失败时,此问题将再次出现,但失败[FacebookSDKManager logOut]
后您没有,应用程序将缓存用户 A 的 accessToken,然后用户 A 将 facebook 帐户更改为用户 B,当用户 B 再次登录 facebook 时,会再次出现这个问题。
回答by Kunal Gupta
I was also stuck on this issue for a while. then i created object for FBSDKLoginManager in .h file and in viewDidLoad of .m file , initialize it and set logout property.
我也被这个问题困住了一段时间。然后我在 .h 文件和 .m 文件的 viewDidLoad 中为 FBSDKLoginManager 创建了对象,初始化它并设置注销属性。
_fbLogin= [[FBSDKLoginManager alloc]init];
[_fbLogin logOut];
This helped me and hope helps you as well. All the best.
这对我有帮助,希望对你也有帮助。祝一切顺利。
Thanks
谢谢