xcode Facebook SDK 3.1 - com.facebook.sdk 使用 [facebook authorize:permissions] 进行身份验证时出现错误 5

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13496007/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:09:10  来源:igfitidea点击:

Facebook SDK 3.1 - com.facebook.sdk Error 5 when authenticating with [facebook authorize:permissions]

iosxcodefacebooksdkauthorization

提问by Atahan Bozkurt

When authenticating with following authorization method i'm getting com.facebook.sdk error 5 with startWithGraphPath and startForMeWithCompletionHandler but not with requestWithGraphPath. I'm succesfully getting token (printing in didLogin) and getting anything which i want with requestwithGraphPath but i'm unable to get work with other methods. If anybody encountered with same issue or something similar or has any idea, i'd be happy if you share it.

当使用以下授权方法进行身份验证时,我使用 startWithGraphPath 和 startForMeWithCompletionHandler 获得 com.facebook.sdk 错误 5,但使用 requestWithGraphPath 时却没有。我成功地获得了令牌(在 didLogin 中打印)并通过 requestwithGraphPath 获得了我想要的任何东西,但我无法使用其他方法。如果有人遇到相同的问题或类似的问题或有任何想法,如果您分享它,我会很高兴。

Thanks

谢谢

Method:

方法:

NSArray *permissions = [[NSArray alloc] initWithObjects: @"user_likes",@"offline_access",@"read_stream",@"publish_stream",nil];
[_facebook authorize:permissions];

采纳答案by Atahan Bozkurt

Following solution worked for me. But if you're storing a permanent access token, you need to be sure user not removed application permissions otherwise it will give an error. And you can check that with requestWithGraphPath -> "me/permissions".

以下解决方案对我有用。但是,如果您要存储永久访问令牌,则需要确保用户没有删除应用程序权限,否则会出错。您可以使用 requestWithGraphPath -> "me/permissions" 进行检查。

Application Init Function (e.g.:didFinishLaunchingWithOptions/or where you init your Facebook object which needs to be fbsessiondelegate in the mean time)

应用程序初始化函数(例如:didFinishLaunchingWithOptions/或者你在哪里初始化你的 Facebook 对象,它需要同时是 fbsessiondelegate

...    
NSArray* permissions = [[NSArray alloc] initWithObjects:@"user_likes",@"offline_access", nil];

        FBSession*oursession = [[FBSession alloc] initWithPermissions:permissions];
...

FBDidLogin function:

FBDid登录功能:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
    [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
    [defaults synchronize];

Example graph api request function:

图 api 请求函数示例:

NSUserDefaults *userDefaults =[NSUserDefaults standardUserDefaults];
        NSString *key = [userDefaults stringForKey:@"FBAccessTokenKey"];

    FBRequest* ourcon = [[FBRequest alloc] initWithSession:oursession graphPath:@"me/likes" parameters:params HTTPMethod:@"GET"];

            [ourcon startWithCompletionHandler: ^(FBRequestConnection *connection, id<FBGraphUser> result, NSError *error){
                if(error)
                {
                    //NSLog(error.code);
                    return;
                }

                NSArray* collection = (NSArray*)[result data];
                NSLog(@"You have %d like", [collection count]);

                NSDictionary* name = [collection objectAtIndex:14];
                NSLog(@"Like Name: %@", [name objectForKey:@"name"]);
            }];

回答by C Abernathy

The startWithGraphPath and other start* methods are likely not picking up an active session. Those methods are relying on an active session being set. See:

startWithGraphPath 和其他 start* 方法可能没有选择活动会话。这些方法依赖于正在设置的活动会话。看:

https://developers.facebook.com/docs/reference/ios/3.1/class/FBRequestConnection#startWithGraphPath%3AcompletionHandler%3A

https://developers.facebook.com/docs/reference/ios/3.1/class/FBRequestConnection#startWithGraphPath%3AcompletionHandler%3A

"The request uses the active session represented by [FBSession activeSession]."

“该请求使用由 [FBSession activeSession] 表示的活动会话。”

So you'll have to do something like this:

所以你必须做这样的事情:

[FBSession setActiveSession:session];

Where session is an FBSession that you had previously set up.

其中 session 是您之前设置的 FBSession。