2

我正在尝试使用 RestKit 进行基本身份验证,但我需要一些帮助来弄清楚为什么我得到状态码 401(未授权)。我试过这个,但它对我不起作用。如果有人能指出我的错误,我将不胜感激。

这是我的数据在服务器端的样子:示例

{
  "Id": "baec6f47",
  "TokenValidity": "00:00:00.1234567",
  "ValidTo": "2013-10-28T23:54:21.2934278+00:00",
  "Parameters": {},
  "Token": "sample string 3"
}

我有一个名为 TokenObject 的 NSObject 类,我需要在其中存储这些数据。

令牌对象.h

#import <Foundation/Foundation.h>

@interface TokenObject : NSObject
@property (nonatomic,strong) NSNumber *tokenId;
@property (nonatomic,strong) NSDate *TokenValidity;
@property (nonatomic,strong) NSDate *ValidTo;
@property (nonatomic,strong)NSString *Parameters;
@property (nonatomic,strong)NSString *Token;

@end

快速概览:

在登录屏幕中,我只询问用户名,一旦用户点击注册,我发送一个 Post 请求来创建帐户并发送一个获取令牌的请求(并在获取请求之前使用 RestKit 的 setAuthorizationHeaderWithUsername )。对于用户名:密码,我使用昵称和硬件 ID。

这是我发布帐户(作品),将用户名和密码编码为base 64(作品)并尝试获取令牌(这是我遇到问题的部分)的方法。

登录视图控制器.h

#import <UIKit/UIKit.h>

@interface LoginViewController : UIViewController<UITextFieldDelegate,NSURLConnectionDelegate>

@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;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
@property(nonatomic,strong)NSString *encodedName;
@property(nonatomic,strong)NSString *encodedHardwareId;
@property(nonatomic,strong)NSData *UserNameEncoding;
@property(nonatomic,strong)NSData *HardwareIdEncoding;
@end

登录视图控制器.m

    //url for creating the account /Accounts

    //url for getting the token /Accounts/Token

   -(void)loadPostRequest
{

    _StoreIdentifierForVendor = [[[UIDevice currentDevice]identifierForVendor]UUIDString];
    _StoreTheModel = [UIDevice currentDevice].model;
    _nickname = usernameTextField.text;

    // AccountsClass is where I store the login information.
     AccountsClass *AccountInfo = [[AccountsClass alloc] init];
    AccountInfo.NickName = _nickname;
    AccountInfo.HardwareId =[[[UIDevice currentDevice]identifierForVendor]UUIDString];
    AccountInfo.DeviceType =[UIDevice currentDevice].model;

//    NSLog(@"HardwareId test %@",AccountInfo.HardwareId);

                /// ********* I can create accounts successfully ********* ///

    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];
    RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"https://picquizstage.azurewebsites.net"]];
                                [manager addRequestDescriptor:requestDescriptor];
                                [manager addResponseDescriptor:AccountDescriptor];
                                // POST to create
    [manager postObject:AccountInfo path:@"/Accounts" parameters:nil success: ^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
        RKLogConfigureByName("*", RKLogLevelTrace); // set all logs to trace,

        /// This is where I setup data for base 64 encoding ///

//        NSData *UserNameEncoding =[_nickname dataUsingEncoding:NSUTF8StringEncoding];
//        NSData *HardwareIdEncoding = [_StoreIdentifierForVendor dataUsingEncoding:NSUTF8StringEncoding];
        UserNameEncoding =[_nickname dataUsingEncoding:NSUTF8StringEncoding];
        HardwareIdEncoding = [_StoreIdentifierForVendor dataUsingEncoding:NSUTF8StringEncoding];
        //encoding
//        NSString *encodedName = [UserNameEncoding base64EncodedString];
//        NSString *encodedHardwareId = [HardwareIdEncoding base64EncodedString];
        encodedName =[UserNameEncoding base64EncodedString];
        encodedHardwareId =[HardwareIdEncoding base64EncodedString];

        //encoding test
        //    NSAssert([encodedName isEqualToString:@"WVlZWQ=="], @"output id for the nickname is failed");
        //    NSAssert([encodedHardwareId isEqualToString:@"OUU0RDY0NzUtN0UwOS00RDY3LUJBNEItRTcyRjEyMzg5QUZC"], @"output test for the hardware id is failed");
        //    RKLogConfigureByName("*", RKLogLevelTrace); // set all logs to trace,
        /*
         ******* TOKEN OBJECT MAPPING BEGINS HERE (PROBLEMATIC PART) *******

         */
        RKObjectMapping *TokenMapping = [RKObjectMapping mappingForClass:[TokenObject class]];
        [TokenMapping addAttributeMappingsFromDictionary:@{@"tokenId":@"Id",@"TokenValidity":@"TokenValidity",@"ValidTo":@"ValidTo",@"Parameters":@"Parameters",@"Token":@"Token"}];
        NSIndexSet *statusCodes2 = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
        RKResponseDescriptor *responseDescriptor =[RKResponseDescriptor responseDescriptorWithMapping:TokenMapping method:RKRequestMethodGET pathPattern:@"/Accounts/Token" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
        RKObjectMapping *errorMapping =[RKObjectMapping mappingForClass:[RKErrorMessage class]];
        // The entire value at the source key path containing the errors maps to the message
        [errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"message"]];

        // Any response in the 4xx status code range with an "errors" key path uses this mapping
        RKResponseDescriptor *errorDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:TokenMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"errors" statusCodes:statusCodes2];
        // Add our descriptors to the manager
        RKObjectManager *manager2 = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"https://picquizstage.azurewebsites.net"]];
        [manager2 addResponseDescriptorsFromArray:@[ responseDescriptor, errorDescriptor ]];
        [manager2.HTTPClient setAuthorizationHeaderWithUsername:encodedName password:encodedHardwareId];
        [manager2 getObjectsAtPath:@"/Accounts/Token" parameters:nil success: ^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

            TokenObject *myObjects =[[TokenObject alloc]init];
            NSLog(@"The tokenId is successful %@",myObjects.tokenId);
            NSLog(@"The ValidTo is successful %@",myObjects.ValidTo);
            NSLog(@"The TokenValidity is successful %@",myObjects.TokenValidity);
            RKLogConfigureByName("*", RKLogLevelTrace); // set all logs to trace,


        }
                           failure:^(RKObjectRequestOperation *operation, NSError *error) {
                               RKLogConfigureByName("*", RKLogLevelTrace); // set all logs to trace,
                           }
         ];

    } failure:nil];

            }

控制台输出

  2013-10-29 09:46:47.641 GuessTheImage[9651:70b] I restkit:RKLog.m:33 RestKit logging initialized...
2013-10-29 09:47:20.035 GuessTheImage[9651:70b] LoginViewController - Submit Action 
2013-10-29 09:47:20.108 GuessTheImage[9651:70b] I restkit.network:RKObjectRequestOperation.m:150 POST 'https://picquizstage.azurewebsites.net/Accounts'
2013-10-29 09:47:20.572 GuessTheImage[9651:4d0f] I restkit.network:RKObjectRequestOperation.m:220 POST 'https://picquizstage.azurewebsites.net/Accounts' (201 Created / 1 objects) [request=0.4625s mapping=0.0015s total=0.5414s]
2013-10-29 09:47:20.580 GuessTheImage[9651:70b] T restkit.network:RKObjectRequestOperation.m:148 GET 'https://picquizstage.azurewebsites.net/Accounts/Token':
request.headers={
    Accept = "application/json";
    "Accept-Language" = "en;q=1, fr;q=0.9, de;q=0.8, zh-Hans;q=0.7, zh-Hant;q=0.6, ja;q=0.5";
    Authorization = "Basic Vm05c2EyOW1aZz09Ok5rTkJOa015UXpVdE5qVTJOaTAwTVRFekxVSXpSVGt0TUVWRFJUQTBOak5EUWpJMg==";
    "User-Agent" = "GuessTheImage/1.0 (iPhone Simulator; iOS 7.0.3; Scale/2.00)";
}
request.body=(null)
2013-10-29 09:47:20.677 GuessTheImage[9651:4d0f] 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=0x8c546f0 {NSLocalizedRecoverySuggestion=, AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest: 0x8ba00e0> { URL: https://quizstage.azurewebsites.net/Acounts/Tokn }, NSErrorFailingURLKey=https://quizstage.azurewebsites.net/Acounts/Tokn, NSLocalizedDescription=Expected status code in (200-299), got 401, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x8c6fb90> { URL: https://quizstage.azurewebsites.net/Acounts/Tokn } { status code: 401, headers {
    "Cache-Control" = "no-cache";
    "Content-Length" = 0;
    Date = "Tue, 29 Oct 2013 16:47:20 GMT";
    Expires = "-1";
    Pragma = "no-cache";
    Server = "Microsoft-IIS/8.0";
    "Www-Authenticate" = "Basic Scheme='PizQuiz' location=http://{0}/Acount/Tokn";
    "X-AspNet-Version" = "4.0.30319";
    "X-Powered-By" = "ASP.NET";
} }}
2013-10-29 09:47:20.679 GuessTheImage[9651:4d0f] E restkit.network:RKObjectRequestOperation.m:208 GET 'https://picquizstage.azurewebsites.net/Acounts/Tokn' (401 Unauthorized / 0 objects) [request=0.0968s mapping=0.0000s total=0.0986s]:
error=Error Domain=org.restkit.RestKit.ErrorDomain Code=-1011 "Expected status code in (200-299), got 401" UserInfo=0x8c546f0 {NSLocalizedRecoverySuggestion=, AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest: 0x8ba00e0> { URL: https://picquizstage.azurewebsites.net/Acounts/Tokn }, NSErrorFailingURLKey=https://picquizstage.azurewebsites.net/Acounts/Tokn, NSLocalizedDescription=Expected status code in (200-299), got 401, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x8c6fb90> { URL: https://picquizstage.azurewebsites.net/Acounts/Tokn } { status code: 401, headers {
    "Cache-Control" = "no-cache";
    "Content-Length" = 0;
    Date = "Tue, 29 Oct 2013 16:47:20 GMT";
    Expires = "-1";
    Pragma = "no-cache";
    Server = "Microsoft-IIS/8.0";
    "Www-Authenticate" = "Basic Scheme='PizQuiz' location=http://{0}/Acount/Tokn";
    "X-AspNet-Version" = "4.0.30319";
    "X-Powered-By" = "ASP.NET";
} }}
response.body=
4

1 回答 1

3

从您的最后一条评论中,这一行:

[manager2.HTTPClient setAuthorizationHeaderWithUsername:@"encodedName" password:@"encodedHardwareId"];

是问题,因为您实际上并没有传递encodedNameandencodedHardwareId变量,而是传递了 2 个常量字符串。所以应该是:

[manager2.HTTPClient setAuthorizationHeaderWithUsername:encodedName password:encodedHardwareId];
于 2013-10-29T15:23:58.233 回答