如何在ios中实现刷新令牌
Posted
技术标签:
【中文标题】如何在ios中实现刷新令牌【英文标题】:How to implement refresh token in ios 【发布时间】:2018-04-06 10:29:31 【问题描述】:只有一些 api 调用需要令牌。当 401 发生时,将进行刷新令牌调用。对于每次调用,令牌都会刷新。 401发生时如何同步执行1个以上的api
【问题讨论】:
【参考方案1】:这取决于你如何设计流程,但我在 Objective C 中做了几乎相同的问题
在我的例子中调用方法 userProfileGETRequest 在调用 userProfileGETRequest 之前检查令牌是否过期(在您的情况下可能是状态码 == 401) 如果令牌没有过期,在我的例子中只需调用 API userProfileAPI 与最后一个令牌 如果令牌过期,则调用刷新令牌并带有成功和失败回调如果成功刷新令牌,则使用更新的刷新令牌调用 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 方法
+ (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);
];
【讨论】:
以上是关于如何在ios中实现刷新令牌的主要内容,如果未能解决你的问题,请参考以下文章