objective-c - 无法使用访问 token 创建 Facebook 用户

标签 objective-c facebook parse-platform stackmob

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

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

最佳答案

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

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

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

关于objective-c - 无法使用访问 token 创建 Facebook 用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14120904/

相关文章:

android - 使用 Facebook 登录并在移动应用程序中进行用户注册

php - 处理访问 token 的 Facebook PHP SDK

ios - PFUsers 共享所有相同的 PFObjects?

ios - .m 中@interface 和@implementation 大括号之间的区别

ios - xcode 将不同的最大字符长度设置为不同的 TextField

ios - 如何使用未知 key 解析JSON?

php - 如何在 facebook Graph API 中使用 Javascript 取消评论

java - 用 fragment 解析

android - 在android中从解析中删除特定行没有成功

ios - 从另一个类的委托(delegate)中访问 IBOutlet