我正在尝试使用 RestKit 通过 github 对用户进行身份验证,并将响应保存回 Core data 以保留 oauth 令牌。响应返回 200 或 201,这很好,但没有任何内容保存回上下文。我认为我缺少一些小东西,但我无法弄清楚。这是发生了什么:
这发生在 viewDidLoad
RKObjectManager *objectManager = [RKObjectManager sharedManager];
RKManagedObjectStore *managedObjectStore = [RKManagedObjectStore defaultStore];
RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:@"SLAuthentication" inManagedObjectStore:managedObjectStore];
NSString *clientID = [(SLAppDelegate *)[[UIApplication sharedApplication] delegate] clientID];
[entityMapping addAttributeMappingsFromDictionary:@{@"id" : @"tokenID", @"url" : @"url", @"token" : @"oauthToken", @"note" : @"note"}];
entityMapping.identificationAttributes = @[@"tokenID"];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:entityMapping method:RKRequestMethodPUT pathPattern:[NSString stringWithFormat:@"/authorizations/clients/%@", clientID] keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];
这是发出请求的地方
NSManagedObjectContext *context = [[[RKObjectManager sharedManager] managedObjectStore] mainQueueManagedObjectContext];
NSString *secret = [(SLAppDelegate *)[[UIApplication sharedApplication] delegate] clientSecret];
NSString *clientID = [(SLAppDelegate *) [[UIApplication sharedApplication] delegate] clientID];
[[RKObjectManager sharedManager].HTTPClient setAuthorizationHeaderWithUsername:userName password:pass];
[[RKObjectManager sharedManager] setRequestSerializationMIMEType:@"application/json"];
[[RKObjectManager sharedManager] putObject:nil path :[NSString stringWithFormat:@"/authorizations/clients/%@", clientID] parameters:@{@"client_secret": secret, @"scopes" : @[@"repo"]} success:nil failure:nil];
//Flush the username and password so that we aren't storing them anymore.
[[RKObjectManager sharedManager].HTTPClient clearAuthorizationHeader];
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"SLUser"];
NSError *error = nil;
NSArray *users = [context executeFetchRequest:request error:&error];
[[users firstObject] setUserName:userName];
[context save:&error];
但是什么都没有回来。API的描述在这里:http: //developer.github.com/v3/oauth/#get-or-create-an-authorization-for-a-specific-app