[英]FBSession fails to open for the first time
I've got a problem when i try to open facebook session for the first time - i get facebook error 5 (not authorised). 我第一次尝试打开Facebook会话时遇到问题-我收到Facebook错误5(未授权)。 In fact, i do not get this popup where user is asked for permissions. 实际上,我没有在询问用户权限的地方弹出此窗口。 However if i stop the app and run it again, everything works fine (i see the popup and there is no fb error 5). 但是,如果我停止应用程序并再次运行它,则一切正常(我看到弹出窗口,并且没有fb错误5)。 I can't find any reason for such behavior. 我找不到这种行为的任何原因。
if ([FBSession.activeSession isOpen]) {
[FBSession openActiveSessionWithReadPermissions:@[@"basic_info", @"email"] allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
if (error) {NSLog(@"error"); return;}
[self sessionStateChanged:session state:state error:nil];
}];
}
- (void)sessionStateChanged:(FBSession *)session state:(FBSessionState)state error:(NSError *)error {
switch (state) {
case FBSessionStateOpen: {
[FBSession setActiveSession:session];
[FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSLog(@"test");
}
break;
}
// do something else
}
Error 5: 错误5:
Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)" UserInfo=0x165b1b10 {com.facebook.sdk:HTTPStatusCode=400, com.facebook.sdk:ParsedJSONResponseKey={
body = {
error = {
code = 190;
"error_subcode" = 458;
message = "Error validating access token: The user has not authorized application 1111111111.";
type = OAuthException;
};
};
code = 400;
}, com.facebook.sdk:ErrorSessionKey=<FBSession: 0x1664ad70, state: FBSessionStateClosed, loginHandler: 0x0, appID: 111111111, urlSchemeSuffix: , tokenCachingStrategy:<FBSessionTokenCachingStrategy: 0x16576320>, expirationDate: (null), refreshDate: (null), attemptedRefreshDate: 0001-12-30 00:00:00 +0000, permissions:(null)>}
Add the following code along with ur code. 将以下代码与ur代码一起添加。 Help it will resolve ur problem. 帮助它将解决您的问题。
-(void)openSession
{
NSArray *permissions = [[NSArray alloc] initWithObjects:
@"user_birthday",
@"friends_birthday",
nil];
[FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:YES
completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error)
{
//[self.sessionStateChanged:session state:state error:error];
}];
}
Go to AppDelegate.m
File and Add the given below Code- 转到AppDelegate.m
文件并添加下面的代码-
-(void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state){
case FBSessionStateOpen:
if (!error)
{
// We have a valid session
NSLog(@"User session found");
}
break;
case FBSessionStateClosed:
NSLog(@"sesion close");
break;
case FBSessionStateClosedLoginFailed:
NSLog(@"sesssion failed");
[FBSession.activeSession closeAndClearTokenInformation];
break;
default:
break;
}
[[NSNotificationCenter defaultCenter]
postNotificationName:UIDocumentStateChangedNotification
object:session];
if (error) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}
-(BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI
{
NSArray *permissions = [[NSArray alloc] initWithObjects:
@"user_location",
@"user_birthday",
@"user_likes",
@"friends_birthday",
nil];
return [FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:allowLoginUI
completionHandler:^(FBSession *session,
FBSessionState state,
NSError *error)
{
[self sessionStateChanged:session
state:state
error:error];}];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.