AFOAuth2Manager 是 AFNetworking 的一个扩展,它简化了对 OAuth 2 提供商的身份验证过程。
NSURL *baseURL = [NSURL URLWithString:@"http://example.com/"];
AFOAuth2Manager *OAuth2Manager =
[[AFOAuth2Manager alloc] initWithBaseURL:baseURL
clientID:kClientID
secret:kClientSecret];
[OAuth2Manager authenticateUsingOAuthWithURLString:@"/oauth/token"
username:@"username"
password:@"password"
scope:@"email"
success:^(AFOAuthCredential *credential) {
NSLog(@"Token: %@", credential.accessToken);
}
failure:^(NSError *error) {
NSLog(@"Error: %@", error);
}];
AFHTTPSessionManager *manager =
[[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
[manager.requestSerializer setAuthorizationHeaderFieldWithCredential:credential];
[manager GET:@"/path/to/protected/resource"
parameters:nil
progress:nil
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"Success: %@", responseObject);
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"Failure: %@", error);
}];
[AFOAuthCredential storeCredential:credential
withIdentifier:serviceProviderIdentifier];
AFOAuthCredential *credential =
[AFOAuthCredential retrieveCredentialWithIdentifier:serviceProviderIdentifier];
AFOAuth2Manager 所有版本的文档在 CocoaDocs 上可供使用。
AFOAuth2Manager 在 MIT 许可下使用。有关更多信息,请参阅 LICENSE 文件。