ios - AFOAuth2Client 和刷新 token

标签 ios ipad oauth-2.0 afnetworking

如何在 iPad 应用中实现 Oauth?

AFOAuth2Client在oauth 2.0中是如何管理刷新token机制的?

有什么方法可以在类内部实现,还是必须自己实现?如何查看token是否过期?

最佳答案

我解决这个问题的方法是用一个代码块包装我的所有请求,如果需要,它将刷新访问 token ,例如

为成功和失败 block 添加一些类型定义:

typedef void (^YFRailsSaasApiClientSuccess)(AFJSONRequestOperation *operation, id responseObject);
typedef void (^YFRailsSaasApiClientFailure)(AFJSONRequestOperation *operation, NSError *error);

那么请求方法是:

- (void)getProductsWithSuccess:(YFRailsSaasApiClientSuccess)success failure:(YFRailsSaasApiClientFailure)failure {
    NSLog(@"getProductsWithSuccess");

    success = ^(AFJSONRequestOperation *operation, id responseObject) {
        [self getPath:@"api/1/products"
           parameters:nil
              success:^(AFHTTPRequestOperation *operation, id responseObject) {
                  NSLog(@"getProductsWithSuccess: success");

                  // TODO: handle response

                  if (success) {
                      success((AFJSONRequestOperation *)operation, responseObject);
                  }
              } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                  NSLog(@"getProductsWithSuccess: failure");
              if (failure) {
                  failure((AFJSONRequestOperation *)operation, error);
              }
          }];
    };

    [self refreshAccessTokenWithSuccess:success failure:failure];
}

检查 token 是否过期并在需要时刷新它的方法是:

- (void)refreshAccessTokenWithSuccess:(YFRailsSaasApiClientSuccess)success failure:(YFRailsSaasApiClientFailure)failure {
    NSLog(@"refreshAccessTokenWithSuccess");

    if (self.credential == nil) {
        if (failure) {
            NSMutableDictionary *errorDetail = [NSMutableDictionary dictionary];
            [errorDetail setValue:@"Failed to get credentials" forKey:NSLocalizedDescriptionKey];
            NSError *error = [NSError errorWithDomain:@"world" code:200 userInfo:errorDetail];
            failure(nil, error);
        }
        return;
    }  

    if (!self.credential.isExpired) {
        NSLog(@"refreshAccessTokenWithSuccess: credential has not expired");

        if (success) {
            success(nil, nil);
        }
        return;
    }

    NSLog(@"refreshAccessTokenWithSuccess: refreshing credential");

    [self authenticateUsingOAuthWithPath:@"oauth/token"
                            refreshToken:self.credential.refreshToken
                                 success:^(AFOAuthCredential *newCredential) {
                                     NSLog(@"Successfully refreshed OAuth credentials %@", newCredential.accessToken);
                                     self.credential = newCredential;
                                     [AFOAuthCredential storeCredential:newCredential
                                                     withIdentifier:self.serviceProviderIdentifier];

                                     if (success) {
                                         success(nil, nil);
                                     }
                                 }
                                 failure:^(NSError *error) {
                                     NSLog(@"An error occurred refreshing credential: %@", error);
                                     if (failure) {
                                         failure(nil, error);
                                     }
                                 }];
}

完整的源代码在 GitHub 上:https://github.com/yellowfeather/rails-saas-ios .

关于ios - AFOAuth2Client 和刷新 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14639189/

相关文章:

azure - 使用 Azure AD 应用程序 (OAuth2) 访问 Microsoft API

ios - UITableView 部分中的背景图像隐藏部分标题

ios - iOS 6 Beta 3 (ipad 3) 中的 UIAlertView 似乎没有触发委托(delegate)中的代码

iphone - iOS:如何获取未处理的 std::exception 的堆栈跟踪?

iphone - 应用程序的iTunes文件共享卡在两者之间

node.js - NodeJS OAuth2.0原理

iphone - UIImage 和 "back"按钮都出现在我的 viewController 中

ios - UIView 忽略 XCode 8 中的背景颜色(或布局问题)

ios - 如何检查 iOS 11 安装的设备是否支持 ARKit?

api - 我应该使用 OAuth 让 SPA 与后端通信吗?