具体说明见新浪官方文档
http://open.weibo.com/wiki/Oauth2/access_token
 
具体实现

第一步 打开回调页面

// 宏定义client_id

#define kClientId @“xxx"

// 宏定义回调地址

#define kRedirect_Uri @"xxx”

// 宏定义client_secret

#define kClient_Secret @“xxx"

1、添加webView

UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];

webView.delegate = self;

[self.view addSubview:webView];

2、加载授权页面 

NSString *oauthPath = [NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@", kClientId, kRedirect_Uri];

NSURL *url = [NSURL URLWithString:oauthPath];

NSURLRequest *request = [NSURLRequest requestWithURL:url];

[webView loadRequest:request];

/**

*  当webView发送一个请求之前都会先调用这个方法, 询问代理可不可以加载这个页面(请求)

*

*  @return YES : 可以加载页面,  NO : 不可以加载页面

*/

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

// 第二步 取得请求参数

// 1.请求返回的URL的绝对路径 http://www.baidu.com/?code=a0872f25a06acf714a4067266a2eeaf4

NSString *requestUrlPath = request.URL.absoluteString;

// 2.获取 ?code= 的范围

NSRange range = [requestUrlPath rangeOfString:@"?code="];

// 3.如果在requestUrlPath中找到了?code= 进行截取

if (range.length) {// if (range.location != NSNotFound) 同左

// 在?code=位置上加上?code=的长度即为?code=后面的字符串

int loc = range.location + range.length;

NSString *code = [requestUrlPath substringFromIndex:loc];

// 第三步 通过POST传递参数体获得accessToken

  // AFNetworking框架

  // 创建请求管理对象

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

// post方式访问需要传递参数体,拼接需要传递的参数

NSMutableDictionary *askParams = [NSMutableDictionary dictionary];

askParams[@"client_id"] = kClientId;

askParams[@"client_secret"] = kClient_Secret;

askParams[@"grant_type"] = @"authorization_code";

askParams[@"code"] = code;

askParams[@"redirect_uri"] = kRedirect_Uri;

// 发送post请求

[manager POST:@"https://api.weibo.com/oauth2/access_token" parameters:askParams success:^(AFHTTPRequestOperation *operation, id responseObject) {

XYLog(@"%@--responseObject", responseObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

XYLog(@"%@--error", error);

}];

}

return YES;

}

POST请求需要拼接的参数包括

/**

client_id string 申请应用时分配的AppKey。

client_secret string 申请应用时分配的AppSecret。

grant_type string 请求的类型,填写authorization_code

code           string 调用authorize获得的code值。

redirect_uri string 回调地址,需需与注册应用里的回调地址一致。

*/  (参见  http://open.weibo.com/wiki/Oauth2/access_token)

返回值 例子如下

{

"access_token": "ACCESS_TOKEN",

"expires_in": 1234,

"remind_in":"798114",

"uid":"12341234"

}

最新文章

  1. 6.Linux的文件权限与目录配置
  2. Android(shape.xml)
  3. POJ 1012 Joseph
  4. 【Android】Handler、Looper源码分析
  5. 利用SQL语句查询数据库中所有表
  6. 【动态规划】HDU 1081 & XMU 1031 To the Max
  7. HDU 3016 Man Down (线段树+dp)
  8. Oracle EBS-SQL (WIP-7):检查当月任务发放记录.sql
  9. HDU1506 ( Largest Rectangle in a Histogram ) [dp]
  10. iOS开发中NSDate时间戳的转换--
  11. poj1260
  12. ios根据颜色返回图片
  13. Spring框架---Spring入门
  14. Java简单工厂模式以及来自lambda的优化
  15. SpringMVC整合Shiro权限框架
  16. input事件--->按键事件的基本实现
  17. MySql常用操作语句(1:启动、连接数据库及用户操作)
  18. Sql查询某个字段是否包含小写字母
  19. [dev] udp socket的read长度问题
  20. SQLServer · 最佳实践 · 透明数据加密TDE在SQLServer的应用

热门文章

  1. [Xcode 实际操作]八、网络与多线程-(10)使用异步Get方式查询GitHub数据
  2. pycharm中模块matplolib生成图表出现中文乱码解决方法
  3. 【微服务】Dubbo初体验
  4. elasticsearch bigdesk 安装和使用
  5. TYVJ1424占卜DIY
  6. CSS之html元素与body元素的范围
  7. salt命令
  8. PyAudio 实现录音 自动化交互实现问答
  9. CF #541div2 D
  10. 洛谷 P2623 物品选取