如何在ios中实现刷新令牌

时间:2018-04-06 10:29:31

标签: ios swift swift3 rx-swift rx-cocoa

只有一些api调用需要令牌。当401发生时,将发生刷新令牌调用。对于每次调用,令牌都是刷新的。如何在401发生时同步执行多个api

1 个答案:

答案 0 :(得分:0)

这取决于你,你如何设计流程,但我做了几乎相同的问题,如在Objective C中的跟随

  • 在我的案例 userProfileGETRequest
  • 中调用类似的方法
  • 在调用 userProfileGETRequest 之前,如果令牌过期,则检查日期(在您的情况下可能是状态代码== 401)
  • 如果令牌未过期,只需在我的案例中使用最后一个令牌 userProfileAPI 调用API
  • 如果令牌已过期,则调用刷新令牌并使用成功和失败回拨
  • 如果成功刷新令牌,请使用更新的刷新令牌调用 userProfileAPI API。

        + (void) userProfileGETRequest:(NSDictionary *)headerParams urlQuery: (NSString*)action parameters:(NSDictionary*)params docOpenPassword: (NSString*)password docOpenOtp: (NSString*)otp
                onComplete:(void (^)(id json, id code, id url))successBlock
                   onError:(void (^)(id error, id code, id url))errorBlock {if ([[SingletonSDK sharedInstance] isTokenExpired:[NSDate date]]) {[self refereshToken:nil :^(id json, id code) {
        [[SingletonSDK sharedInstance] handleLoginResponseObject:json];
    
            [self userProfileAPI:headerParams urlQuery:action parameters:params
    
                      onComplete:^(id json, id code, id url) {
                          successBlock(json, code, url);
                      } onError:^(id error, id code, id url) {
                          errorBlock(error, code, url);
                      }];
    
        } onError:^(id error, id code) {
            [[SingletonSDK sharedInstance] hideProgessHud];
            return ;
        }];
    }
    
    } else {
        [self userProfileAPI:headerParams urlQuery:action parameters:params
                  onComplete:^(id json, id code, id url) {
                      successBlock(json, code, url);
                  } onError:^(id error, id code, id url) {
                      errorBlock(error, code, url);
                  }];
    }}
    

// userProfileAPI Methods

+ (void) userProfileAPI:(NSDictionary *)headerParams urlQuery: (NSString*)action parameters:(NSDictionary*)params 
                                         onComplete:(void (^)(id json, id code, id url))successBlock
                                            onError:(void (^)(id error, id code, id url))errorBlock
{

NSString *authorizationValue = [self setAuthorizationValue:action];
NSString *language = [self editedLanguageNameAsApiRequired];

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

//set headers values
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:language forHTTPHeaderField:@"Accept-Language"];
[manager.requestSerializer setValue:authorizationValue forHTTPHeaderField:@"authorization"];

[manager GET:action parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"getRequest response success");
    NSString *url = [[[operation response] URL] absoluteString];
    NSInteger statusCode = [operation.response statusCode];
    NSNumber *statusObject = [NSNumber numberWithInteger:statusCode];

    successBlock(responseObject, statusObject, url);
}
     failure:^(AFHTTPRequestOperation *operation, NSError *error)
 {
     NSString *url = [[[operation response] URL] absoluteString];
     NSInteger statusCode = [operation.response statusCode];
     NSNumber *statusObject = [NSNumber numberWithInteger:statusCode];
     if ([self takeDesiredActionIfAccessTokenExpired:statusCode]) {
         return ;
     }
     id responseObject = operation.responseData;
     id json = nil;
     id errorMessage = nil;

     if ([statusObject integerValue] == 404) {
         errorMessage = [[SingletonSDK sharedInstance] getStringValueFromLanguageKey: COMMON_ERROR_SHARED_PREFERENCES];//NSLocalizedString(COMMON_ERROR_RESOURCE_NOT_FOUND, nil);
     } else {
         if (responseObject) {

             json = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:&error];
             errorMessage = [(NSDictionary*)json objectForKey:@"Message"];

         }else{

             json = [error.userInfo objectForKey:NSLocalizedDescriptionKey];
             errorMessage = json;

         }
     }


     if(![errorMessage isKindOfClass:[NSString class]]){
         errorMessage = [[SingletonSDK sharedInstance] getStringValueFromLanguageKey: COMMON_ERROR_MSG] ; //NSLocalizedString(COMMON_ERROR_MSG, nil);
     }

 }];
}