Reputation: 4695
I need to create a new photo album using the Facebook SDK on iOS. My code was working, but has recently started returning errors like this:
{
"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;
};
}
According to https://developers.facebook.com/docs/reference/api/errors/ , and error with code 2 is: "Server-side problem; app should retry after waiting, up to some app-defined threshold".
This has been failing for at least a few days though, and I can't seem to find anyone else having similar trouble, so I'm assuming the problem is my own.
Any ideas what could be the cause? Relevant code is below. The createFacebookAlbum
method is the one that is failing.
- (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;
}
}
Upvotes: 3
Views: 1238
Reputation: 156
I had the same problem as you.
One month ago, I could create albums without user_photos
.
But now not anymore.
I add the permission: user_photos
and it works again.
See also that article on facebook about change to permissions https://developers.facebook.com/docs/reference/api/album/
Upvotes: 4