1

我正在使用 restkit 登录和映射 highrise api 的对象。我可以使用 /me.xml 登录,但之后卡住以获取其他端点,如 people.xml、tasks.xml 等。

登录代码:

//MYObjectManager is a class inherited from RKObjectManager.

NSURL *url = [NSURL URLWithString:BASE_URL];    
MYObjectManager *sharedManager  = [self managerWithBaseURL:url];
sharedManager.requestSerializationMIMEType = RKMIMETypeXML;
[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:RKMIMETypeXML];
[sharedManager setAcceptHeaderWithMIMEType:@"application/xml"];
[sharedManager.HTTPClient setAuthorizationHeaderWithUsername:USERNAME password:PASSWORD];

从 MYObjectManager 继承的 UserManager 类,用于将经过身份验证的用户加载为:

    - (void) loadAuthenticatedUser:(void (^)(User *))success failure:(void (^)(RKObjectRequestOperation *, NSError *))failure {
        RKResponseDescriptor *authenticatedUserResponseDescriptors = [RKResponseDescriptor responseDescriptorWithMapping:[MappingProvider userMapping] method:RKRequestMethodGET pathPattern:nil keyPath:@"user" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

       [self addResponseDescriptor:authenticatedUserResponseDescriptors];

       [self getObjectsAtPath:@"me.xml" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
        if (success) {
            User *currentUser = (User *)[mappingResult.array firstObject];            
            success(currentUser);
          }
        } failure:^(RKObjectRequestOperation *operation, NSError *error) {
          if (failure) {
            failure(operation, error);
        }
   }];
}

这有效并将令牌返回给我。但是现在使用这个令牌,我也无法发出进一步的请求(似乎某些 cookie 不是由 restkit 设置的,可能是) ContactManager 也继承自 MYObjectManager。

在用户的成功块中,我向 people.xml 发出请求:

[[UserManager sharedManager] loadAuthenticatedUser:^(User *user) {
    self.apiToken = user.utoken;
    if(self.apiToken){
       // [[ContactManager sharedManager].HTTPClient setDefaultHeader:@"Authorization" value: [NSString stringWithFormat:@"Basic %@", self.apiToken]];

        [[ContactManager sharedManager].HTTPClient setAuthorizationHeaderWithToken:[NSString stringWithFormat:@"Basic %@", self.apiToken]];

        [[ContactManager sharedManager] loadContacts:^(ContactList *contacts) {
            NSLog(@"contacts is: %@", contacts);
        }failure:^(RKObjectRequestOperation *operation, NSError *error){
            NSLog(@"contacts error occured: %@", error);
        }];
    }
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"login error occured: %@", error);
}];

但这给出了 401 未经授权的错误。什么不见​​了?任何帮助表示赞赏。

编辑 - 请求和响应如下:

为登录而发送的请求标头

restkit.network:RKObjectRequestOperation.m:148 GET 'https://dws10.highrisehq.com/me.xml':
request.headers={
    Accept = "application/xml";
    "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 ZGlwaWthLmFiYmFkOmRpcGlrYTEyMw==";
    "User-Agent" = "TemplateApp/1.0 (iPhone Simulator; iOS 7.0.3; Scale/2.00)";
}

收到的响应标头:

restkit.network:RKObjectRequestOperation.m:218 GET 'https://dws10.highrisehq.com/me.xml' (200 OK / 1 objects) [request=27.9217s mapping=0.0433s total=28.4159s]:
response.headers={
    "Cache-Control" = "private, max-age=0, must-revalidate";
    Connection = "keep-alive";
    "Content-Length" = 447;
    "Content-Type" = "application/xml; charset=utf-8";
    Date = "Thu, 19 Jun 2014 13:41:48 GMT";
    Etag = "\"6315ed097b16e809367d5e5243e719ea\"";
    Server = nginx;
    Status = "304 Not Modified";
    "Strict-Transport-Security" = "max-age=31536000";
    "Timing-Allow-Origin" = "*";
    "X-Frame-Options" = SAMEORIGIN;
    "X-Request-Id" = ee68caa2005ffad955f12175a619b0f3;
    "X-Runtime" = 94;
    "X-Throttle-Count" = 1;
    "X-Throttle-Horizon" = "2014-06-19T13:41:50Z";
    "X-Throttle-Max" = 500;
}

response.body=<?xml version="1.0" encoding="UTF-8"?>
<user>
  <created-at type="datetime">2014-06-19T09:39:44Z</created-at>
  <id type="integer">1045054</id>
  <token>my token value here</token>
  <updated-at type="datetime">2014-06-19T09:39:44Z</updated-at>
  <dropbox>dropbox@09235795.dws10.highrisehq.com</dropbox>
  <name>Dipika Abbad</name>
  <email-address>abbad.deepika@gmail.com</email-address>
  <admin type="boolean">true</admin>
</user>

我发送的人的请求标头是:

restkit.network:RKObjectRequestOperation.m:148 GET 'https://dws10.highrisehq.com/people.xml':
request.headers={
    Accept = "application/xml";
    "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 <my api token from above response body>";
    "User-Agent" = "TemplateApp/1.0 (iPhone Simulator; iOS 7.0.3; Scale/2.00)";
}

收到的回复:

response.body=HTTP Basic: Access denied.
2014-06-19 19:19:31.797 TemplateApp[3403:a0b] contacts error occured: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1011 "Expected status code in (200-299), got 401" UserInfo=0xbd6f8d0 {NSLocalizedRecoverySuggestion=HTTP Basic: Access denied.
, AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest: 0xb9e3b60> { URL: https://dws10.highrisehq.com/people.xml }, NSErrorFailingURLKey=https://dws10.highrisehq.com/people.xml, NSLocalizedDescription=Expected status code in (200-299), got 401, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xbd8b0a0> { URL: https://dws10.highrisehq.com/people.xml } { status code: 401, headers {
    "Cache-Control" = "no-cache";
     Connection = "keep-alive";
    "Content-Length" = 27;
    "Content-Type" = "text/html; charset=utf-8";
    Date = "Thu, 19 Jun 2014 13:49:30 GMT";
    Server = nginx;
    Status = "401 Unauthorized";
    "Strict-Transport-Security" = "max-age=31536000";
    "Www-Authenticate" = "Basic realm=\"Application\"";
    "X-Frame-Options" = SAMEORIGIN;
    "X-Request-Id" = 9a420bfa16fa4620eeb9469e5ad7345d;
    "X-Runtime" = 5;
    "X-Throttle-Count" = 1;
    "X-Throttle-Horizon" = "2014-06-19T13:49:40Z";
    "X-Throttle-Max" = 500;
} }}
4

2 回答 2

0

将以下内容放入您的应用委托中:

RKLogConfigureByName("RestKit/Network", RKLogLevelTrace); 
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);

并查看控制台以检查请求并验证您是否设置了正确的标头

于 2014-06-19T13:14:03.913 回答
0

问题解决了。它需要将用户名设置为令牌,密码设置为虚拟令牌。

于 2014-06-23T09:15:18.940 回答