对于Iphone的新浪微博客户端其实现基本流程如下
1.根据Appkey 请求 http://api.t.sina.com.cn/oauth/requtst_token 获取未授权的requestToken
2.根据requestToken给用户提示授权,请求http://api.t.sina.com.cn/oauth/authorize 获取用户对我们应用的授权码
3.根据授权码和requestToken,请求http://api.t.sina.com.cn/oauth/access_token 获取访问用户信息的accessToken
4.每次使用AccessToken访问Api的各接口url
#pragma mark 第一步获取requestToken
- (IBAction)startSina:(id)sender {
OAConsumer *consumer = [[OAConsumer alloc] initWithKey:APPKEY secret:APPSECRET];
OAHMAC_SHA1SignatureProvider *hmacSha1Provider = [[OAHMAC_SHA1SignatureProvider alloc] init];
OAMutableURLRequest *hmacSha1Request = [[OAMutableURLRequest alloc] initWithURL:[NSURL URLWithString:RequestURL]
consumer:consumer
token:NULL
realm:NULL
signatureProvider:hmacSha1Provider
nonce:[self _generateNonce]
timestamp:[self _generateTimestamp]];
[hmacSha1Request setHTTPMethod:@"GET"];
OADataFetcher *fetcher = [[OADataFetcher alloc] init];
[fetcher fetchDataWithRequest:hmacSha1Request
delegate:self
didFinishSelector:@selector(requestTokenTicket:finishedWithData:)
didFailSelector:@selector(requestTokenTicket:failedWithError:)];
}
#pragma mark 第二步 弹出web url让用户授权
- (void)requestTokenTicket:(OAServiceTicket *)ticket finishedWithData:(NSMutableData *)data {
NSString *responseBody = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(@"获得未授权的KEY:%@",responseBody);
OAToken *token = [[OAToken alloc] initWithHTTPResponseBody:responseBody];
NSString *tt = [token.key URLEncodedString];
NSString *url = [NSString stringWithFormat:@"%@?oauth_token=%@&oauth_callback=%@",AuthorizeURL,tt,CallBackURL];
//callBack URL我们希望是自己的窗口可以写成oauth://mysnia.com
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
NSUserDefaults *info = [NSUserDefaults standardUserDefaults];
[info setValue:responseBody forKey:@"responseBody"];
[info synchronize];
#pragma mark url回调 oauth第三步 获取授权码 之后获取AccessToken
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
NSLog(@"获得已授权的key:%@",[url query]);
NSUserDefaults *info = [NSUserDefaults standardUserDefaults];
//解析回调的URL获取oauth_verifier
//回调的URL需要在.plist中加入URL schemes :oauth和URL identifier:mysnia.com 你懂的...
NSString *string = [[url query] substringWithRange:NSMakeRange([[url query] length]-6, 6)];
OAConsumer *consumer = [[OAConsumer alloc] initWithKey:APPKEY secret:APPSECRET];
NSLog(@"利用数据持久取得第二步获得的token");
OAToken *token = [[OAToken alloc] initWithHTTPResponseBody:[info valueForKey:@"responseBody"]];
OAHMAC_SHA1SignatureProvider *hmacSha1Provider = [[OAHMAC_SHA1SignatureProvider alloc] init];
OAMutableURLRequest *hmacSha1Request = [[OAMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?oauth_verifier=%@",AccessURL,string]]
consumer:consumer
token:token
realm:NULL
signatureProvider:hmacSha1Provider
nonce:[self _generateNonce]
timestamp:[self _generateTimestamp]];
[hmacSha1Request setHTTPMethod:@"GET"];
OADataFetcher *fetcher = [[OADataFetcher alloc] init];
[fetcher fetchDataWithRequest:hmacSha1Request
delegate:self
didFinishSelector:@selector(requestTokenTicket:finishedWithData:)
didFailSelector:@selector(requestTokenTicket:failedWithError:)];
return YES;
}
- (void)requestTokenTicket:(OAServiceTicket *)ticket finishedWithData:(NSMutableData *)data {
NSString *responseBody = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(@"完成最后一步,并根据结果生成token:%@",responseBody);
OAToken *token = [[OAToken alloc] initWithHTTPResponseBody:responseBody];
NSLog(@"得到之后api用的token:%@",token);
//恭喜,得到了AccessToken,您应该把他存到sqlite或某个地方,以后操作该账户就不用获取了
}
该项目代码找我的发布资源http://download.csdn.net/source/3541585