在iOS上打开包含现有令牌数据的FBSession

时间:2013-07-26 13:46:48

标签: ios facebook facebook-ios-sdk

我有一个后端服务,它将在我的应用程序中登录并返回之前登录的扩展facebook令牌,可能来自其他设备(因此此设备上没有缓存凭据)。我想使用此令牌来启动Facebook会话。我使用的是:

FBSession* session = [[FBSession alloc] initWithPermissions:@[@"publish_actions"]];
FBAccessTokenData* tokenData = 
[FBAccessTokenData createTokenFromString:token
                             permissions:@[@"publish_actions"]
                          expirationDate:nil
                               loginType:FBSessionLoginTypeTestUser
                             refreshDate:nil];


[session openFromAccessTokenData:tokenData completionHandler:nil];

为了清楚起见,我在这里传递'nil',在我的代码中,我处理完成并登录返回的会话对象,该对象似乎处于打开状态且没有错误。当我尝试使用会话时,我收到如下错误:

Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)" UserInfo=0xb8b1680 {com.facebook.sdk:HTTPStatusCode=403, com.facebook.sdk:ParsedJSONResponseKey={
    body =     {
        error =         {
            code = 200;
            message = "(#200) The user hasn't authorized the application to perform this action";
            type = OAuthException;
        };
    };
    code = 403;
}, com.facebook.sdk:ErrorSessionKey=<FBSession: 0xa2b62a0, state: FBSessionStateOpen, loginHandler: 0xa29e9b0, appID: 131721883664256, urlSchemeSuffix: , tokenCachingStrategy:<FBSessionTokenCachingStrategy: 0xa273f60>, expirationDate: 4001-01-01 00:00:00 +0000, refreshDate: 2013-07-26 16:21:10 +0000, attemptedRefreshDate: 0001-12-30 00:00:00 +0000, permissions:(
    email,
    "publish_actions"
)>}

有任何建议......?

3 个答案:

答案 0 :(得分:5)

我找到了一个解决方案:(前提是你有一个现有的令牌)

首先将FBSessionTokenCachingStrategy类子类化为MySessionTokenCachingStrategy并覆盖以下方法:

- (FBAccessTokenData *)fetchFBAccessTokenData 
{
    NSMutableDictionary *tokenInformationDictionary = [NSMutableDictionary new];

    // Expiration date
    tokenInformationDictionary[@"com.facebook.sdk:TokenInformationExpirationDateKey"] = [NSDate dateWithTimeIntervalSinceNow: 3600];

    // Refresh date
    tokenInformationDictionary[@"com.facebook.sdk:TokenInformationRefreshDateKey"] = [NSDate date];

    // Token key
    tokenInformationDictionary[@"com.facebook.sdk:TokenInformationTokenKey"] = self.token;

    // Permissions
    tokenInformationDictionary[@"com.facebook.sdk:TokenInformationPermissionsKey"] = self.permissions;

    // Login key
    tokenInformationDictionary[@"com.facebook.sdk:TokenInformationLoginTypeLoginKey"] = @0;

    return [FBAccessTokenData createTokenFromDictionary: tokenInformationDictionary];
}

然后使用上面的类创建

MySessionTokenCachingStrategy* tokenCachingStrategy = 
[[MySessionTokenCachingStrategy alloc] initWithToken:token
                                      andPermissions:@[@"read_stream"]];



FBSession *session = [[FBSession alloc] initWithAppID: nil
                                          permissions: @[@"read_stream"]]
                                      urlSchemeSuffix: nil
                                   tokenCacheStrategy: tokenCachingStrategy];

    if (session.state == FBSessionStateCreatedTokenLoaded)
    {
        // Set the active session
        [FBSession setActiveSession: session];

        // Open the session, but do not use iOS6 system acount login
        // if the caching strategy does not store info locally on the
        // device, otherwise you could use:
        // FBSessionLoginBehaviorUseSystemAccountIfPresent
        [session openWithBehavior: FBSessionLoginBehaviorWithFallbackToWebView
                completionHandler: ^(FBSession *session,
                                     FBSessionState state,
                                     NSError *error) {
                    if (!error)
                    {
                        if (session.isOpen)
                        {
                            successBlock();
                        }
                    }
                    else
                    {
                        failureBlock([error description]);
                    }
                }];
    }

答案 1 :(得分:0)

这不是因为您首先需要一些读取权限,而只有那时您可以要求其他一些吗?

答案 2 :(得分:0)

你正在做[[FBSession alloc] initWithPermissions:@[@"publish_actions"]],这意味着你的会话只会问及&#34;有&#34; publish_actions的权限,但请查看错误中的令牌:

com.facebook.sdk:ErrorSessionKey=<FBSession: 0xa2b62a0, state: FBSessionStateOpen, loginHandler: 0xa29e9b0, appID: 131721883664256, urlSchemeSuffix: , tokenCachingStrategy:<FBSessionTokenCachingStrategy: 0xa273f60>, expirationDate: 4001-01-01 00:00:00 +0000, refreshDate: 2013-07-26 16:21:10 +0000, attemptedRefreshDate: 0001-12-30 00:00:00 +0000, permissions:(
   email,
   "publish_actions"
)

您的令牌也具有email权限。你认为这可能是问题吗?

尝试使用[[FBSession alloc] initWithPermissions:@[@"publish_actions", @"email"]]进行实例化,并检查是否仍有问题。