AFOAuth1Client是为简化对AFNetworking进行OAuth 1.0a授权的提供了扩展。
NSURL *baseURL = [NSURL URLWithString:@"https://twitter.com/oauth/"];
AFOAuth1Client *OAuth1Client = [[AFOAuth1Client alloc] initWithBaseURL:baseURL
key:@"..."
secret:@"..."];
将您的应用程序在自定义URL方案中注册,并使用该路径/success
作为回调URL。自定义URL方案的回调应发送通知,该通知将完成OAuth交易。
NSURL *callbackURL = [NSURL URLWithString:@"x-com-YOUR-APP-SCHEME://success"];
[OAuth1Client authorizeUsingOAuthWithRequestTokenPath:@"/request_token"
userAuthorizationPath:@"/authorize"
callbackURL:callbackURL
accessTokenPath:@"/access_token"
success:^(AFOAuth1Token *accessToken) {
NSLog(@"Success: %@", accessToken);
}
failure:^(NSError *error) {
NSLog(@"Error: %@", error);
}];
在iOS上响应自定义URL方案
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)URL
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
NSNotification *notification =
[NSNotification notificationWithName:kAFApplicationLaunchedWithURLNotification
object:nil
userInfo:@{kAFApplicationLaunchOptionsURLKey: URL}];
[[NSNotificationCenter defaultCenter] postNotification:notification];
return YES;
}
Mattt Thompson
AFOAuth1Client采用MIT许可证。有关更多信息,请参阅LICENSE文件。