0

我正在尝试将 Facebook 与我的 iOS 应用程序集成,但遇到了这个障碍。

当我第一次运行此代码时,它运行良好,但似乎 facebook 缓存中的某些内容导致了此错误:“必须使用活动访问令牌来查询有关当前用户的信息。”

我使用这两个函数来尝试上传照片:

+(BOOL)logInAndPostImage:(UIImage*)image andMessage:(NSString*)message {

    NSArray *permissions = [NSArray arrayWithObjects:@"publish_stream", nil];
    [FBSession.activeSession closeAndClearTokenInformation];
    return [FBSession  openActiveSessionWithPublishPermissions:permissions
                                              defaultAudience:FBSessionDefaultAudienceOnlyMe
                                                 allowLoginUI:YES
                                            completionHandler:
            ^(FBSession *session, FBSessionState state, NSError *error) {
                [self sessionStateChanged:session
                                    state:state
                                    error:error];
                if(error) {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Problem connecting with Facebook" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
                    [alert show];
                } else {
                    [self postImage:image andMessage:message];
                }
            }];
}

+(void)postImage:(UIImage*)image andMessage:(NSString*)message
{

    NSLog(@" Access Token Description: %@",[[FBSession activeSession] accessToken].description);

    [FBRequestConnection startForUploadPhoto:image completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
        if (!error) {
            [[[UIAlertView alloc] initWithTitle:@"Result"
                                        message:@"Your update has been posted to Facebook!"
                                       delegate:self
                              cancelButtonTitle:@"Sweet!"
                              otherButtonTitles:nil] show];
        } else {
            [[[UIAlertView alloc] initWithTitle:@"Error"
                                        message:@"Yikes! Facebook had an error.  Please try again!"
                                       delegate:nil
                              cancelButtonTitle:@"Ok"
                              otherButtonTitles:nil] show];
            NSLog(@"%@", error);
        }

    }];
}

当我查询活动令牌描述的描述时,它实际上是空的。所以我猜虽然FBSession openActiveSessionWithPublishPermissions:defaultAudience:allowLoginUI:completionHandler:没有返回错误,但我没有生成令牌?

关于如何进一步调试此错误的任何建议?我已经按照 Facebook 开发者页面列出的所有附加设置进行了操作。

4

1 回答 1

0

您的会话很可能实际上没有打开。你不应该打电话

[self postImage:image andMessage:message];

除非没有错误并且

(state == FBSessionStateOpen)

要进一步调试,只需查看“状态”变量的值,看看它是什么。

需要注意的另一件事是,如果这是用户第一次登录您的应用程序,您不能请求发布/写入权限。您必须首先请求读取权限,然后在上下文中请求发布权限。如果您最初要求发布权限,您应该会收到错误消息。由于您没有收到错误,因此可能并非如此。

于 2013-05-22T00:47:17.117 回答