2

I am writing a method which gets the facebook user records from my db on Parse and then recreates the records on StackMob. When I call createUserWithFacebookToken it returns this error:

[48887:c07] Error Domain=HTTP Code=401 "The operation couldn’t be completed. (HTTP error 401.)" UserInfo=0xa6a9e60 {error=Login failed: }

- (IBAction)fetchAllUsers:(id)sender {
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://api.parse.com/1/users/"]];
    [request setValue:@"XXXX" forHTTPHeaderField:@"X-Parse-Application-Id"];
    [request setValue:@"XXXX" forHTTPHeaderField:@"X-Parse-Master-Key"];

    NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"didReceiveResponse");
    [self.responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"didFailWithError");
    NSLog(@"Connection failed: %@", [error description]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"connectionDidFinishLoading");
    NSLog(@"Succeeded! Received %d bytes of data",[self.responseData length]);

    // convert to JSON
    NSError *myError = nil;
    NSDictionary *res = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:&myError];
    NSArray *allUsers = [res objectForKey:@"results"];
    // show all values
    for (int i=0;i<allUsers.count;i++) {
        id value = [allUsers objectAtIndex:i];
        NSString *username = [value objectForKey:@"username"];
        NSDictionary *authData = [value objectForKey:@"authData"];
        NSDictionary *facebook = [authData objectForKey:@"facebook"];
        NSString *accessToken = [facebook objectForKey:@"access_token"];
        if (accessToken) {
            NSDictionary *aUser = [NSDictionary dictionaryWithObjectsAndKeys:accessToken, @"accessToken", username, @"username", nil];
//            [_userInfo addObject:aUser];

            NSLog(@"%@", aUser);
            [self processTheUser:aUser];
        }
    }
}

- (void)processTheUser:(NSDictionary *)user {
    [self.client createUserWithFacebookToken:[user valueForKey:@"accessToken"] username:[user valueForKey:@"username"] onSuccess:^(NSDictionary *result) {
        NSLog(@"%@", result);
    } onFailure:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}
ctatti
  • 170
  • 1
  • 1
  • 11
  • I copied the wrong error message: this is the right one. `[48887:c07] Error Domain=HTTP Code=401 "The operation couldn’t be completed. (HTTP error 401.)" UserInfo=0xa6a9e60 {error=Login failed: }` – ctatti Jan 02 '13 at 15:57
  • You should edit the question to include the correct error message – combinatorial Jan 03 '13 at 02:16
  • question updated with correct error message. – ctatti Jan 04 '13 at 14:11

3 Answers3

2

Error 401 corresponds to stackmob error code SMErrorUnauthorized. I have found that you get this response in a call to createUserWithFacebookToken when the user already exists on stackmob, so in my error handler I call loginWithFacebookToken. If this call also fails then I attempt to reauthorize with facebook...

[FBSession.activeSession closeAndClearTokenInformation];
[[FBSession class] performSelector:@selector(renewSystemAuthorization)];
[FBSession openActiveSessionWithReadPermissions:nil
                                   allowLoginUI:YES
                              completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {

and call createUserWithFacebookToken again on success in the completion handler.

combinatorial
  • 9,132
  • 4
  • 40
  • 58
1

The clue is in the error:

facebook token already registered for another user

I am not totally familiar with StackMob, but I use Parse a lot: I assume like Parse, StackMob won't let you create two users with identical Facebook credentials. This makes sense, because otherwise single-sign-on with your Facebook account wouldn't work!

So what's probably happening is you already have a user linked to that Facebook account in StackMob, and it's not letting you add another one. You may want to add a conditional test to check whether a user already exists in StackMob before trying to create them.

lxt
  • 31,146
  • 5
  • 78
  • 83
1

Problem solved! I was working on users with a expired access_token. I simply get the valid ones and all is ok!

ctatti
  • 170
  • 1
  • 1
  • 11