2

我正在编写一种方法,该方法从我的 Parse 数据库中获取 facebook 用户记录,然后在 StackMob 上重新创建记录。当我调用 createUserWithFacebookToken 它返回此错误:

[48887:c07] 错误域 = HTTP 代码 = 401“操作无法完成。(HTTP 错误 401。)”用户信息 = 0xa6a9e60 {错误 = 登录失败:}

- (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);
    }];
}
4

3 回答 3

2

错误 401 对应于 stackmob 错误代码SMErrorUnauthorized。我发现createUserWithFacebookToken当用户已经存在于 stackmob 上时,您会在调用中获得此响应,因此在我的错误处理程序中我调用loginWithFacebookToken. 如果此调用也失败,那么我尝试使用 facebook 重新授权...

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

并在完成处理程序中再次调用 createUserWithFacebookToken 成功。

于 2013-01-03T06:08:38.433 回答
1

线索在错误中:

facebook token already registered for another user

我对 StackMob 并不完全熟悉,但我经常使用 Parse:我假设像 Parse 一样,StackMob 不会让您创建两个具有相同 Facebook 凭据的用户。这是有道理的,因为否则使用您的 Facebook 帐户进行单点登录将无法正常工作!

因此,可能发生的情况是,您已经有一个用户链接到 StackMob 中的该 Facebook 帐户,并且不允许您添加另一个用户。在尝试创建用户之前,您可能需要添加条件测试以检查用户是否已存在于 StackMob 中。

于 2013-01-02T11:07:13.823 回答
1

问题解决了!我正在处理 access_token 过期的用户。我只是得到有效的,一切都好!

于 2013-01-05T20:35:10.247 回答