zoukankan      html  css  js  c++  java
  • iOS AFOAuth2Manager使用心得

    github地址:  https://github.com/AFNetworking/AFOAuth2Manager

    这个库,不多说,实现OAuth 2.0授权访问。

    确实可以减轻很大的负担,而且使用很容易。

    完成 OAuth 2.0授权认证 大概就这几步:

    1.按照后台给的参数,向服务器请求token等数据。

    2.存储返回的数据,并记录token过期的时间,用于后续步骤判定token是否过期。(如果是拿到便开始使用,可以省略存储这一步)

    3.按照后台给的格式,将token写入Request的Header里的Authorization。

    4.每次请求数据时,都用这个已经在header写入了token的Request。

    5.后续使用中,随时判定token有效期。若token过期时,马上获取新的token,再写入Authorization。

    下面是我使用的大致步骤(仅供参考): 

    NSURL *baseURL = [NSURL URLWithString:@"http://xxx.xxx.x.x:xxxx"];
        AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL];
        
        [manager POST:@"/OAuth/token"
           parameters:@{@"client_id":@"xxx",
                        @"client_secret":@"xxx",
                        @"grant_type":@"xxx",
                        }
              success:^(AFHTTPRequestOperation *operation, id responseObject) {
                  NSLog(@"
    =============accessToken=================
    
    %@
    
    ",responseObject);
                  
                  //! 存储credential,内含access_token
                  AFOAuthCredential *credential = [[AFOAuthCredential alloc]initWithOAuthToken:[responseObject objectForKey:@"access_token"]
                                                                                     tokenType:[responseObject objectForKey:@"token_type"]];
                  [credential setExpiration:[responseObject objectForKey:@"expires_in"]];
                  
                  //! 记录token过期时间,这里的token有效时间为1800s
                  _tokenInvalidateTime = [NSDate dateWithTimeIntervalSinceNow:1770];
                  
                  //! 设置需要在Headers中加入的参数Authorization,_opManager为另一个AFHTTPRequestOperationManager,因为我这个例子里,二者使用的baseURL不同
                  _opManager.requestSerializer = [AFHTTPRequestSerializer serializer];
                  [_opManager.requestSerializer setAuthorizationHeaderFieldWithCredential:credential];
                  //! 我这里并没有用作者提供的方法,将credential写入文件,
                 //!  因为我是直接将Authorization的设置放在了这里完成
                 //!  如果,我没有用到这_opManager
                 //!  这里我就应该用[AFOAuthCredential storeCredential:credential withIdentifier:serviceProviderIdentifier];
                 //!  这个方法将credential写入文件,
                 //! 之后用AFOAuthCredential *credential =
                //![AFOAuthCredential retrieveCredentialWithIdentifier:serviceProviderIdentifier];
                 //!  方法在其他地方重新获取这个credential。credential里就还有token等信息
              }
              failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                  NSLog(@"
    AccessToken获取失败: %@", error);
              }]; 
  • 相关阅读:
    了解教育网访问情况
    .NET开源社区存在的问题
    欢迎大家谈谈Windows Live Mail desktop的使用感受
    [公告]新版排行榜页面发布
    国外技术新闻[来自Digg.com]
    首页小改进
    [SQL Server 2005]String or binary data would be truncated
    Linux获得真正3D桌面 开源支持者喜不自禁
    新版 .net开发必备10大工具
    使用新类型Nullable处理数据库表中null字段
  • 原文地址:https://www.cnblogs.com/ficow/p/5351086.html
Copyright © 2011-2022 走看看