我正在尝试进行基本身份验证,一旦获得令牌,我将需要使用base 64编码(对于编码,我检查了IOS7的新数据方法,但我将无法使用它它只是IOS 7)。到目前为止,我能够根据问题Link to question基于此问题创建一个不成功的请求。因此,我无法获得令牌。
问题
进行基本身份验证然后使用base 64 enconding与RestKit的实用方法是什么?
应用概述
在我的登录屏幕中,我只询问用户名,一旦用户登录,我就向服务器发送POST请求以创建帐户。在同一方法中,我使用不同的URL为基本身份验证创建另一个Get请求。
这是验证数据的样子:
{
"Id": "255ab054",
"TokenValidity": "00:00:00.1234567",
"ValidTo": "2013-10-24T21:30:28.3295551+00:00",
"Parameters": {},
"Token": "sample string 3"
}
AccountClass.h
#import <Foundation/Foundation.h>
@interface AccountsClass : NSObject
@property (nonatomic, strong,retain)NSString *DeviceType;
@property (nonatomic,strong)NSNumber *AccountId;
@property (nonatomic,strong) NSString *NickName;
@property (nonatomic,strong) NSNumber *HardwareId;
@end
LoginViewController.h
#import <UIKit/UIKit.h>
@interface LoginViewController : UIViewController<UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITextField *usernameTextField;
@property (strong, nonatomic) IBOutlet UIButton *submitButton;
@property (nonatomic,readonly) NSUUID *identifierForVendor;
@property(nonatomic, readonly, retain) NSString *model;
@property (nonatomic,readonly,retain)NSString *StoreIdentifierForVendor;
@property (nonatomic,readonly,retain)NSString *StoreTheModel;
- (IBAction)submit:(id)sender;
@property (nonatomic,strong)NSString *nickname;
@end
然后在LoginViewController.m中,这就是我尝试获取令牌的方式:
[manager postObject:AccountInfo path:@"/Acounts/Tokn" parameters:nil success:nil failure:nil];
[manager.HTTPClient setAuthorizationHeaderWithUsername:@"NickName" password:@"HardwareId"];
获取令牌(我相信,我做错了。)
最后这是我在
中使用的方法LoginViewController.m
-(void)loadPostRequest
{
_StoreIdentifierForVendor = [[[UIDevice currentDevice]identifierForVendor]UUIDString];
_StoreTheModel = [UIDevice currentDevice].model;
_nickname = usernameTextField.text;
AccountsClass *AccountInfo = [[AccountsClass alloc] init];
AccountInfo.NickName = _nickname;
AccountInfo.HardwareId =[[[UIDevice currentDevice]identifierForVendor]UUIDString];
AccountInfo.DeviceType =[UIDevice currentDevice].model;
RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[AccountsClass class]];
[responseMapping addAttributeMappingsFromArray:@[@"NickName", @"HardwareId", @"DeviceType",@"AccountId"]];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
RKResponseDescriptor *AccountDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:statusCodes];
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping]; // objectClass == NSMutableDictionary
[requestMapping addAttributeMappingsFromArray:@[@"NickName", @"HardwareId", @"DeviceType",@"AccountId"]];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[AccountInfo class] rootKeyPath:nil method:RKRequestMethodAny];
// First request is for to create the account using @"/Accounts" extension.
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://quizstage.azurewebsites.net"]];
[manager addRequestDescriptor:requestDescriptor];
[manager addResponseDescriptor:AccountDescriptor];
// POST to create
[manager postObject:AccountInfo path:@"/Acounts" parameters:nil success:nil failure:nil];
//And this is how I try to get the token
[manager getObjectsAtPath:@"http://quizstage.azurewebsites.net/Acounts/Tokn" parameters:nil success:nil failure:nil];
[manager.HTTPClient setAuthorizationHeaderWithUsername:@"NickName" password:@"HardwareId"];
}
这是我在控制台中获得的内容:
2013-10-24 23:17:40.259 GuessTheImage[6603:70b] I restkit:RKLog.m:33 RestKit logging initialized...
2013-10-24 23:17:51.837 GuessTheImage[6603:70b] LoginViewController - Submit Action
2013-10-24 23:17:51.913 GuessTheImage[6603:70b] I restkit.network:RKObjectRequestOperation.m:150 POST 'http://quizstage.azurewebsites.net/Acounts'
2013-10-24 23:17:51.914 GuessTheImage[6603:70b] I restkit.network:RKObjectRequestOperation.m:150 GET 'http://quizstage.azurewebsites.net/Acounts/Tokn'
2013-10-24 23:17:52.187 GuessTheImage[6603:3d0b] E restkit.network:RKObjectRequestOperation.m:547 Object request failed: Underlying HTTP request operation failed with error: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1011 "Expected status code in (200-299), got 401" UserInfo=0x8bc9160 {NSLocalizedRecoverySuggestion=, AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest: 0x8ba1b30> { URL: http://quizstage.azurewebsites.net/Acounts/Tokn }, NSErrorFailingURLKey=http://quizstage.azurewebsites.net/Acounts/Tokn, NSLocalizedDescription=Expected status code in (200-299), got 401, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x8bc7fe0> { URL: http://quizstage.azurewebsites.net/Acounts/Tokn } { status code: 401, headers {
"Cache-Control" = "no-cache";
"Content-Length" = 0;
Date = "Fri, 25 Oct 2013 06:17:52 GMT";
Expires = "-1";
Pragma = "no-cache";
Server = "Microsoft-IIS/8.0";
"Set-Cookie" = "ARRAffinity=9505e3b43afb653bbcf6f6ed272f61c2ad5ee56489dc485c1ec82f9dee0c39b7;Path=/;Domain=quizstage.azurewebsites.net, WAWebSiteSID=ac6f8d6c21cc4e4782c818ce8b7705a7; Path=/; HttpOnly";
"Www-Authenticate" = "Basic Scheme='Quiz' location=http://{0}/Acount/Tokn";
"X-AspNet-Version" = "4.0.30319";
"X-Powered-By" = "ASP.NET, ARR/2.5, ASP.NET";
} }}
2013-10-24 23:17:52.197 GuessTheImage[6603:3d0b] E restkit.network:RKObjectRequestOperation.m:213 GET 'http://picquizstage.azurewebsites.net/Accounts/Token' (401 Unauthorized / 0 objects) [request=0.2724s mapping=0.0000s total=0.3571s]: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1011 "Expected status code in (200-299), got 401" UserInfo=0x8bc9160 {NSLocalizedRecoverySuggestion=, AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest: 0x8ba1b30> { URL: http://picquizstage.azurewebsites.net/Accounts/Token }, NSErrorFailingURLKey=http://quizstage.azurewebsites.net/Acounts/Tokn, NSLocalizedDescription=Expected status code in (200-299), got 401, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x8bc7fe0> { URL: http://quizstage.azurewebsites.net/Acounts/Tokn } { status code: 401, headers {
"Cache-Control" = "no-cache";
"Content-Length" = 0;
Date = "Fri, 25 Oct 2013 06:17:52 GMT";
Expires = "-1";
Pragma = "no-cache";
Server = "Microsoft-IIS/8.0";
"Set-Cookie" = "ARRAffinity=9505e3b43afb653bbcf6f6ed272f61c2ad5ee56489dc485c1ec82f9dee0c39b7;Path=/;Domain=quizstage.azurewebsites.net, WAWebSiteSID=ac6f8d6c21cc4e4782c818ce8b7705a7; Path=/; HttpOnly";
"Www-Authenticate" = "Basic Scheme='Quiz' location=http://{0}/Acount/Tokn";
"X-AspNet-Version" = "4.0.30319";
"X-Powered-By" = "ASP.NET, ARR/2.5, ASP.NET";
} }}
2013-10-24 23:17:52.236 GuessTheImage[6603:3e07] I restkit.network:RKObjectRequestOperation.m:220 POST 'http://quizstage.azurewebsites.net/Acounts' (201 Created / 1 objects) [request=0.3213s mapping=0.0014s total=0.4041s]
我读过可以使用RestKit(无需身份验证)映射数据,但我很乐意使用身份验证进行API调用。我很感激你的帮助。
答案 0 :(得分:0)
您确定要发出返回404的POST请求吗?它应该由您的服务器定义是否应该使用GET或POST。