我需要在 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;
}
}