/*
//OAuth授权过程:
1、登陆账号
http://open.weibo.com
注册一下开发者信息,成为新浪的开发者
2、点击“移动应用”,创建一个应用(勾选应用名称,应用平台就可以了),页面跳转,显示到“开发阶段即可”
3、回到主页面,点击API接口》首页》OAuth2.0授权认证
根据说明点击进入(1)OAuth2/authorize 获取code(使用get请求)
点击进入(2)OAuth2/access_token 获取Token(使用post请求)
4、保存获取的token,供以后获取微博数据使用。
5、返回API首页,进入“微博》statuses/home_timeline”按要求利用token获取微博数据
*/
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 创建web
NSURLRequest * requset = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://api.weibo.com/oauth2/authorize?client_id=3025058065&redirect_uri=www.baidu.com"]];
UIWebView * web = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:web];
[web loadRequest:requset];
web.delegate = self;
}
//每一次获取token都要一个新的code。但是token获取一次就可以了,保存起来。
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
//截取包含code的网址,并取出code
NSString * path = request.URL.absoluteString;
BOOL isCode = [path containsString:@"code="];
NSString * sub;
if (isCode)
{
NSRange range = [path rangeOfString:@"code="];
sub = [path substringFromIndex:range.location+5];
self.code = sub;
//根据code获取token(获取的token保存起来,供下一次运行的时候使用)
[self getToken];
}
return YES;
}
- (void)getToken
{
AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
NSDictionary * dic = @{@"client_id":@"3025058065", @"client_secret":@"867adcfb83eeebe64394629fafefea41", @"grant_type":@"authorization_code", @"code":self.code, @"redirect_uri":@"www.baidu.com"};
manager.responseSerializer = [AFHTTPResponseSerializer serializer];//因为新浪返回的数据没有标记成json类型的,所以这个使用data类型的来接受,否则请求失败。
[manager POST:@"https://api.weibo.com/oauth2/access_token" parameters:dic success:^(AFHTTPRequestOperation *operation, NSData * responseObject) {
//json转换成对象
NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
self.dic = dic;
//获取微博数据
[self getStatus];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
}];
}
- (void)getStatus
{
NSString * token = self.dic[@"access_token"];
NSString * str = [NSString stringWithFormat:@"https://api.weibo.com/2/statuses/home_timeline.json?access_token=%@", token];
//请求微博数据
AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
[manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, NSDictionary * responseObject) {
NSLog(@"-------%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
}];
}
//OAuth授权过程:
1、登陆账号
http://open.weibo.com
注册一下开发者信息,成为新浪的开发者
2、点击“移动应用”,创建一个应用(勾选应用名称,应用平台就可以了),页面跳转,显示到“开发阶段即可”
3、回到主页面,点击API接口》首页》OAuth2.0授权认证
根据说明点击进入(1)OAuth2/authorize 获取code(使用get请求)
点击进入(2)OAuth2/access_token 获取Token(使用post请求)
4、保存获取的token,供以后获取微博数据使用。
5、返回API首页,进入“微博》statuses/home_timeline”按要求利用token获取微博数据
*/
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 创建web
NSURLRequest * requset = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://api.weibo.com/oauth2/authorize?client_id=3025058065&redirect_uri=www.baidu.com"]];
UIWebView * web = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:web];
[web loadRequest:requset];
web.delegate = self;
}
//每一次获取token都要一个新的code。但是token获取一次就可以了,保存起来。
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
//截取包含code的网址,并取出code
NSString * path = request.URL.absoluteString;
BOOL isCode = [path containsString:@"code="];
NSString * sub;
if (isCode)
{
NSRange range = [path rangeOfString:@"code="];
sub = [path substringFromIndex:range.location+5];
self.code = sub;
//根据code获取token(获取的token保存起来,供下一次运行的时候使用)
[self getToken];
}
return YES;
}
- (void)getToken
{
AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
NSDictionary * dic = @{@"client_id":@"3025058065", @"client_secret":@"867adcfb83eeebe64394629fafefea41", @"grant_type":@"authorization_code", @"code":self.code, @"redirect_uri":@"www.baidu.com"};
manager.responseSerializer = [AFHTTPResponseSerializer serializer];//因为新浪返回的数据没有标记成json类型的,所以这个使用data类型的来接受,否则请求失败。
[manager POST:@"https://api.weibo.com/oauth2/access_token" parameters:dic success:^(AFHTTPRequestOperation *operation, NSData * responseObject) {
//json转换成对象
NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
self.dic = dic;
//获取微博数据
[self getStatus];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
}];
}
- (void)getStatus
{
NSString * token = self.dic[@"access_token"];
NSString * str = [NSString stringWithFormat:@"https://api.weibo.com/2/statuses/home_timeline.json?access_token=%@", token];
//请求微博数据
AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
[manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, NSDictionary * responseObject) {
NSLog(@"-------%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
}];
}