面试题答案
一键面试- 获取OAuth 2.0的认证信息:
- 通常需要客户端ID、客户端密钥、授权服务器URL、重定向URI等信息。这些信息一般由服务提供商提供。
- 构建授权请求URL:
- 以OAuth 2.0授权码模式为例,授权请求URL的格式类似:
https://authorization_server_url/authorize?response_type=code&client_id=your_client_id&redirect_uri=your_redirect_uri&scope=required_scopes
- 在Objective - C中,可以使用
NSMutableURLComponents
来构建这个URL。例如:
NSMutableURLComponents *components = [NSMutableURLComponents componentsWithString:@"https://authorization_server_url/authorize"]; components.queryItems = @[ [NSURLQueryItem queryItemWithName:@"response_type" value:@"code"], [NSURLQueryItem queryItemWithName:@"client_id" value:@"your_client_id"], [NSURLQueryItem queryItemWithName:@"redirect_uri" value:@"your_redirect_uri"], [NSURLQueryItem queryItemWithName:@"scope" value:@"required_scopes"] ]; NSURL *authorizationURL = components.URL;
- 以OAuth 2.0授权码模式为例,授权请求URL的格式类似:
- 引导用户进行授权:
- 可以使用
UIApplication.sharedApplication
打开上述构建的授权URL,让用户在浏览器中进行授权操作。
[[UIApplication sharedApplication] openURL:authorizationURL options:@{} completionHandler:nil];
- 可以使用
- 处理授权回调:
- 当用户授权完成后,会重定向到指定的
redirect_uri
,并且在URL的查询参数中包含授权码(code
)。 - 在应用中,需要在
AppDelegate
的application:openURL:options:
方法中捕获这个回调URL,并从中提取授权码。
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options { NSURLComponents *components = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO]; NSString *code = nil; for (NSURLQueryItem *item in components.queryItems) { if ([item.name isEqualToString:@"code"]) { code = item.value; break; } } if (code) { // 在这里可以发起获取令牌的请求 } return YES; }
- 当用户授权完成后,会重定向到指定的
- 使用授权码获取访问令牌:
- 构建获取令牌的请求,一般是向令牌端点(
token_endpoint
)发送POST请求,携带授权码、客户端ID、客户端密钥等信息。 - 使用AFNetworking来发送这个请求:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; manager.requestSerializer = [AFHTTPRequestSerializer serializer]; manager.responseSerializer = [AFJSONResponseSerializer serializer]; NSDictionary *parameters = @{ @"grant_type": @"authorization_code", @"code": code, @"client_id": @"your_client_id", @"client_secret": @"your_client_secret", @"redirect_uri": @"your_redirect_uri" }; [manager POST:@"https://token_endpoint" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSString *accessToken = responseObject[@"access_token"]; // 保存访问令牌,一般可以使用NSUserDefaults等方式 [[NSUserDefaults standardUserDefaults] setObject:accessToken forKey:@"access_token"]; } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"获取令牌失败: %@", error); }];
- 构建获取令牌的请求,一般是向令牌端点(
- 在后续网络请求中使用访问令牌:
- 每次发起网络请求时,在请求头中添加
Authorization
字段,值为Bearer your_access_token
。 - 在AFNetworking中,可以通过设置
requestSerializer
来添加这个请求头:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; NSString *accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"access_token"]; [manager.requestSerializer setValue:[NSString stringWithFormat:@"Bearer %@", accessToken] forHTTPHeaderField:@"Authorization"]; [manager GET:@"your_api_url" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { // 处理成功响应 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { // 处理失败响应 }];
- 每次发起网络请求时,在请求头中添加