使用Open Graph在iOS上使用Facebook SDK创建相册

时间:2013-10-14 03:24:50

标签: ios objective-c facebook-graph-api

我需要在iOS上使用Facebook SDK创建新相册。我的代码工作正常,但最近开始返回这样的错误:

{
    "com.facebook.sdk:ErrorSessionKey" = "<FBSession: 0x7290850, state: FBSessionStateOpenTokenExtended, loginHandler: 0x7290c40, appID: 380841565285975, urlSchemeSuffix: , tokenCachingStrategy:<FBSessionTokenCachingStrategy: 0x8a933e0>, expirationDate: 4001-01-01 00:00:00 +0000, refreshDate: 2013-10-14 03:16:01 +0000, attemptedRefreshDate: 0001-12-30 00:00:00 +0000, permissions:(\n    \"create_note\",\n    \"basic_info\",\n    \"share_item\",\n    \"status_update\",\n    \"publish_actions\",\n    \"video_upload\",\n    email,\n    \"photo_upload\",\n    installed,\n    \"publish_stream\",\n    \"user_birthday\",\n    \"user_location\"\n)>";
    "com.facebook.sdk:HTTPStatusCode" = 500;
    "com.facebook.sdk:ParsedJSONResponseKey" =     {
        body =         {
            error =             {
                code = 2;
                message = "An unexpected error has occurred. Please retry your request later.";
                type = OAuthException;
            };
        };
        code = 500;
    };
}

根据https://developers.facebook.com/docs/reference/api/errors/,代码2的错误是:“服务器端问题;应用程序应在等待后重试,直到某个应用程序定义的阈值”。

这已经失败了至少几天,而且我似乎找不到其他人遇到类似的麻烦,所以我假设问题是我自己的。

任何想法可能是什么原因?相关代码如下。 createFacebookAlbum方法是失败的方法。

- (void)shareToFacebook {
if (FBSession.activeSession.isOpen) {
    NSLog(@"SESSION IS OPEN");

    [self createFacebookAlbum];
} else {
    NSLog(@"SESSION IS NOT OPEN");
    NSArray* permissions = [NSArray arrayWithObject:@"email"];
    [FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
        [self sessionStateChanged:session state:state error:error];

        if (error) {
            /* handle failure */
            NSLog(@"error:%@, %@", error, [error localizedDescription]);
            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"There was a problem with your Facebook permissions." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
        }
        else if (state == FBSessionStateClosed || state == FBSessionStateClosedLoginFailed ) {
            [FBSession.activeSession closeAndClearTokenInformation];
        }
        else if (state == FBSessionStateOpenTokenExtended || state == FBSessionStateOpen) {
            if(!self.presentedFacebookSheet) {
                [self performSelector:@selector(reauthorizeAndContinuePostToFacebook) withObject:nil afterDelay:0.5];
                self.presentedFacebookSheet = YES;
            }
        }
        }];
    }
}

- (void)reauthorizeAndContinuePostToFacebook {
    NSArray *permissions = [NSArray arrayWithObjects:@"publish_actions", nil];

    [[FBSession activeSession] requestNewPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceFriends completionHandler:^(FBSession *session, NSError *error) {
        [self shareToFacebook];
    }];
}

- (void)createFacebookAlbum {
    NSMutableDictionary* parameters = [NSMutableDictionary dictionary];
    [parameters setObject:@"Test name" forKey:@"name"];
    [parameters setObject:@"Test message" forKey:@"message"];

    FBRequest* request = [FBRequest requestWithGraphPath:@"me/albums" parameters:parameters HTTPMethod:@"POST"];

    NSLog(@"creating facebook album");
    FBRequestConnection *connection = [[FBRequestConnection alloc] init];
    [connection addRequest:request
         completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
             if (!error) {
                 NSString* albumId = [result objectForKey:@"id"];
                 NSLog(@"OK %@", albumId);
             }
             else {
                 NSLog(@"Error: %@",error.userInfo);
             }
         }];
    [connection start];
}

- (void)sessionStateChanged:(FBSession *)session state:(FBSessionState) state error:(NSError *)error {
    switch (state) {
        case FBSessionStateOpen:
        {
            [[FBRequest requestForMe] startWithCompletionHandler:
             ^(FBRequestConnection *connection,
               NSDictionary<FBGraphUser> *user,
               NSError *error) {
                 if (error) {
                     //error
                 }
                 else {
                     NSLog(@"User session found");
                 }
             }];
        }
            break;
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
            [FBSession.activeSession closeAndClearTokenInformation];
            break;
        default:
            break;
    }
}

1 个答案:

答案 0 :(得分:4)

我和你有同样的问题。 一个月前,我可以创建没有user_photos的专辑。

但现在不再了。

我添加了权限:user_photos并且它再次有效。

另请参阅关于更改权限的facebook上的文章 https://developers.facebook.com/docs/reference/api/album/