1

我正在尝试进行基本身份验证,一旦获得令牌,我将需要使用 base 64 编码(对于编码,我检查了 IOS7 的新数据方法,但我无法使用它,因为它是 IOS仅 7 个)。到目前为止,我能够根据问题Link to question创建基于此问题的不成功请求。因此,我无法获得令牌。

问题

进行基本身份验证然后使用带有 RestKit 的 base 64 编码的实用方法是什么?

应用概览

在我的登录屏幕中,我只询问用户名,一旦用户登录,我就会向服务器发送一个 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

登录视图控制器.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"];

获得令牌(我相信,我做错了。)

最后这是我使用的方法

登录视图控制器.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 调用。我感谢您的帮助。

4

1 回答 1

0

您确定要发出返回 404 的 POST 请求吗?它应该由您的服务器定义是否应该使用 GET 或 POST。

于 2013-10-25T02:44:20.450 回答