0

我正在尝试使用 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

4

1 回答 1

0

响应是异步的,因此您需要在 put 请求上实现成功(和失败)块并在那里运行您的 fetch 请求。但是,您的获取是针对不同实体的,因此不清楚您如何检查/使用返回的身份验证对象。

如果您仍然有问题,请为映射打开跟踪日志记录以获取更多信息并检查返回到故障块的错误。

于 2013-11-09T16:13:39.620 回答