OAuth是一个关于授权(authorization)的开放网络标准,定义了四种授权方式:
具体的模式介绍就不多说了,我用到的是客户端模式授权,没有用官网提供的几种解决办法,我这里用的是AFOAuth2Manager,实现起来很简单。
github地址: https://github.com/AFNetworking/AFOAuth2Manager
如果你是密码模式,可以直接用下面的方法实现:
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); }];
如果是客户端模式,方法如下,
NSURL *baseURL = [NSURL URLWithString:@"http://example.com/"];
AFOAuth2Manager *OAuth2Manager =
[[AFOAuth2Manager alloc] initWithBaseURL:baseURL
clientID:kClientID
secret:kClientSecret];
[OAuth2Manager authenticateUsingOAuthWithURLString:@"
/oauth/token
" scope:@"scope" success:^(AFOAuthCredential * _Nonnull credential) {
NSLog(@"---成功");
NSLog(@"Token: %@", credential.accessToken);
} failure:^(NSError * _Nonnull error) {
NSLog(@"---失败");
}];
原文:http://www.cnblogs.com/cui-cui/p/7107982.html