IOS OAuth授权分析
一、黑马微博 ---> 用户的微博数据
1.成为新浪的开发者(加入新浪微博的开发阵营)
* 注册一个微博帐号,登录http://open.weibo.com
帐号:643055866@qq.com
密码:ios4762450
* 填写开发者的个人信息(比如姓名、出生日期、上传身份证)
2.创建应用
* 假设应用名称叫做“黑马微博”
* 应用创建完毕,默认就进入“开发”阶段,就具备了授权的资格
* 应用相关数据
App Key:3141202626 // 应用的唯一标识
App Secret:ee9de4d2431be061b22fe328332a5228
Redirect URI:http://www.itheima.com
3.用户对“黑马微博”进行资源授权----OAuth授权2.0
1> 获取未授权的Request Token : 展示服务器提供商提供的登录页面
* URL : https://api.weibo.com/oauth2/authorize
* 参数
client_id true string 申请应用时分配的AppKey // 得知道给哪个应用授权
redirect_uri true string 授权回调地址 // 授权成功后跳转到哪个页面
2> 获取授权过的Request Token
* 授权成功后,自动跳转到回调页面,比如
http://www.itheima.com/?code=eabdc03cc4cc51484111b1cfd9c4cd0b
// 新浪会在回调页面后面拼接一个参数:授权成功后的Request Token
3> 根据授权过的Request Token换取一个Access Token
* URL : https://api.weibo.com/oauth2/access_token
* 参数
client_id true string 申请应用时分配的AppKey。
client_secret true string 申请应用时分配的AppSecret。
grant_type true string 请求的类型,填写authorization_code
code true string 调用authorize获得的code值。
redirect_uri true string 回调地址,需需与注册应用里的回调地址一致
* 返回结果
{
"access_token" = "2.00vWf4GEUSKa7D739148f7608SXA9B";
"expires_in" = 157679999;
"remind_in" = 157679999;
uid = 3758830533;
}
// uid == user_id == 当前登录用户的ID == 用户的唯一标识
{
"access_token" = "2.00vWf4GEUSKa7D739148f7608SXA9B";
"expires_in" = 157679999;
"remind_in" = 157679999;
uid = 3758830533;
}
* access_token和uid的去呗
access_token : 1个用户给1个应用授权成功后,就获得对应的1个access_token,作用是:允许1个应用访问1个用户的数据
uid:1个用户对应1个uid,每1个用户都有自己唯一的uid
举例:
张三
李四
应用1
应用2
张三给应用1、应用2授权成功了:1个uid、2个access_token
李四给应用2授权成功了:1个uid、1个access_token
上面操作:产生了2个uid,3个access_token
二、授权过程中常见错误:
1.invalid_request
1> 没有传递必填的请求参数
2> 请求参数不对
3> URL中间留有空格
2.invalid_client
1> client_id的值传递错误(AppKey不对)
3.redirect_uri_mismatch
1> 回调地址不对
三、授权帐号注意
1.如果应用还没有经过新浪审核,只能访问自己或者其他15个测试帐号的微博数据
授权code (HMOAuthViewController.m)
#import "HMOAuthViewController.h"
#import "MBProgressHUD+MJ.h"
#import "AFNetworking.h"
#import "HMTabBarViewController.h"
#import "HMNewfeatureViewController.h" @interface HMOAuthViewController () <UIWebViewDelegate> @end @implementation HMOAuthViewController
- (void)viewDidLoad
{
[super viewDidLoad]; //1.创建UIWebView
UIWebView * webView=[[UIWebView alloc]init];
webView.frame=self.view.frame;
[self.view addSubview:webView]; //2.加载登录页面
NSURL *url=[NSURL URLWithString:@"https://api.weibo.com/oauth2/authorize?client_id=881257207&redirect_uri=http://blog.sina.com.cn"]; NSURLRequest *request=[NSURLRequest requestWithURL:url];
[webView loadRequest:request]; //3.设置代理
webView.delegate=self;
} #pragma mark - UIWebViewDelegate
/**
*UIWebView开始加载资源的时候调用(开始发送请求)
*
*/
-(void)webViewDidStartLoad:(UIWebView *)webView
{
[MBProgressHUD showMessage:@"正在加载中......"];
}
/**
*UIWebView加载完毕的时候调用(请求完毕)
*
*/
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[MBProgressHUD hideHUD];
}
/**
*UIWebView加载失败的时候调用(请求失败)
*
*/
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[MBProgressHUD hideHUD];
} /**
*UIWebView每当发送一个请求之前,都会先调用这个代理方法(询问代理允不允许加载这个请求)
*
*@param request 即将发送的请求
*@reture Yes 允许加载 NO:禁止加载
*/
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
//1.获得请求地址
NSString *url=request.URL.absoluteString; //2.判断url是否为回调地址
/** ~~~~
url = http://www.itheima.com/?code=a3db74011c311e629bafce3e50c25339
range.location == 0
range.length > 0
*/
/**
url = https://api.weibo.com/oauth2/authorize
range.location == NSNotFound
range.length == 0
*/
NSRange range=[url rangeOfString:@"http://blog.sina.com.cn/?code="];
if(range.location!=NSNotFound)//是回调地址
{
//截取授权成功后的请求标记
int from=range.location+range.length;
NSString *code=[url substringFromIndex:from]; //根据code获得一个accessToken
[self accessTokenWithCode:code]; //禁止加载回调页面
return NO;
} return YES; }
/**
*根据code获得一个accessToken
*
*@param code 授权成功后的请求标记
*/
-(void)accessTokenWithCode:(NSString *)code
{ //1.获得请求管理者
AFHTTPRequestOperationManager *mgr=[AFHTTPRequestOperationManager manager]; //2.封装请求参数
NSMutableDictionary *params=[NSMutableDictionary dictionary];
params[@"client_id"]=@"";
params[@"client_secret"]=@"0b8c34d4659d656834f827abfb3a1805";
params[@"redirect_uri"]=@"http://blog.sina.com.cn";
params[@"grant_type"]=@"authorization_code";
params[@"code"]=code; //3.发送POST请求
[mgr POST:@"https://api.weibo.com/oauth2/access_token" parameters:params success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) { //隐藏HUD
[MBProgressHUD hideHUD];
HMLog(@"请求成功。。。。。"); //存储授权成功的帐号信息
NSString *doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *filepth=[doc stringByAppendingPathComponent:@"account.plist"];
[responseObject writeToFile:filepth atomically:YES]; //切换控制器(可能去新特性\tabBar)
//如何知道第一次使用这个版本?比较上次的使用情况
NSString * versionKey=(__bridge NSString*)kCFBundleVersionKey; //从沙盒中取出上次存储的软件版本号(取出用户上次的使用记录)
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
NSString *lastVersion=[defaults objectForKey:versionKey]; //获得当前打开软件的版本号
NSString *currentVersion=[NSBundle mainBundle].infoDictionary[versionKey]; UIWindow *window=[UIApplication sharedApplication].keyWindow;
if([currentVersion isEqualToString:lastVersion])//当前版本号==上次使用的版本:显示HMTabBarViewController
{
window.rootViewController=[[HMTabBarViewController alloc]init];
}
else{//当前版本号 !=上次使用的版本:显示版本新特性
window.rootViewController=[[HMNewfeatureViewController alloc]init]; //存储这次使用的软件版本
[defaults setObject:currentVersion forKey:versionKey];
[defaults synchronize];
} } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//隐藏HUD
[MBProgressHUD hideHUD];
HMLog(@"请求失败--@",error);
}];
}
/**
Request failed: unacceptable content-type: text/plain
*/
@end
调用code 信息
#import "AppDelegate.h"
#import "HMTabBarViewController.h"
#import "HMNewfeatureViewController.h"
#import "HMOAuthViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { application.statusBarHidden=NO; //1.创建窗口
self.window=[[UIWindow alloc]init];
self.window.frame=[UIScreen mainScreen].bounds; //2.设置窗口的根控制器
NSString *doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *filepath=[doc stringByAppendingPathComponent:@"account.plist"];
NSDictionary *account=[NSDictionary dictionaryWithContentsOfFile:filepath];
if(account){
//如果知道第一次使用这个版本?比较上次的使用情况
NSString *versionKey=(__bridge NSString *)kCFBundleVersionKey; //从沙盒中取出上次存储的软件版本号(取出用户上次的使用记录)
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
NSString *lastVersion=[defaults objectForKey:versionKey]; //获得当前打开软件的版本号
NSString *currentVersion=[NSBundle mainBundle].infoDictionary[versionKey]; if([currentVersion isEqualToString:lastVersion])// 当前版本号 == 上次使用的版本:显示HMTabBarViewController
{
self.window.rootViewController=[[HMTabBarViewController alloc]init];
} else// 当前版本号 != 上次使用的版本:显示版本新特性
{
self.window.rootViewController= [[HMNewfeatureViewController alloc] init]; //存储这次使用的软件版本
[defaults setObject:currentVersion forKey:versionKey];
[defaults synchronize];
} }else{//没有登录过
self.window.rootViewController=[[HMOAuthViewController alloc]init];
} //3.显示窗口(成为主窗口)
[self.window makeKeyAndVisible]; return YES;
}
IOS OAuth授权分析的更多相关文章
- IOS第三天-新浪微博 - 版本新特性,OAuth授权认证
*********版本新特性 #import "HWNewfeatureViewController.h" #import "HWTabBarViewController ...
- iOS之新浪微博的OAuth授权
新浪微博的OAuth授权 之前通过新浪微博开发平台写过微博的的项目,现在就开始总结下各个方面的知识点,一个是为了加深印象.巩固知识,另一个记录自己学习过程,希望自己在开发这条路上有所积累,为以后的道路 ...
- [iOS微博项目 - 2.0] - OAuth授权3步
A.概念 OAUTH协议为用户资源的授权提供了一个安全的.开放而又简易的标准.与以往的授权方式不同之处是OAUTH的授权不会使第三方触及到用户的帐号信息(如用户名与密码),即第三方无需使用用 ...
- OAuth授权过程
什么是OAuth授权? 一.什么是OAuth协议 OAuth(开放授权)是一个开放标准,所谓OAuth(即Open Authorization,开放授权),它是为用户资源授权提供了一种安全简单的标准, ...
- iOS app内存分析套路
iOS app内存分析套路 Xcode下查看app内存使用情况有2中方法: Navigator导航栏中的Debug navigator中的Memory Instruments 一.Debug navi ...
- IOS照片颠倒分析及移动/页面端的处理策略和思路
前言: 前几天, 写了一篇关于IOS手机上传照片颠倒的技术分析文章: IOS照片颠倒分析及PHP服务端的处理. 不过其思路是从服务器来进行处理的, 这种做法相当普遍. 今天来讲述下, 如何从移动端/页 ...
- 什么是OAuth授权?
什么是OAuth授权? 一.什么是OAuth协议 OAuth(开放授权)是一个开放标准. 允许第三方网站在用户授权的前提下访问在用户在服务商那里存储的各种信息. 而这种授权无需将用户提供用户名和密 ...
- 微信订阅号里实现oauth授权登录,并获取用户信息 (完整篇)
摘要 这段时间一直有人问我,订阅号实现的oauth授权登录的问题,之前写的比较简单,很多人不明白.众所周知,微信公众号分订阅号.服务号.企业号:每个号的用途不一样,接口开放程度也不一样.微信还有个扯淡 ...
- 新浪微博客户端(13)-使用UIWebView加载OAuth授权界面
使用UIWebView加载OAuth授权界面 DJOAuthViewController.m #import "DJOAuthViewController.h" @interfac ...
随机推荐
- 逐行创建、读取并写入txt(matlab) && 生成文件夹里文件名的.bat文件
fidin=fopen('C:\Users\byte\Desktop\新建文件夹 (4)\tr4.txt','r'); fidout=fopen('C:\Users\byte\Desktop\新建文件 ...
- JS图片加载失败用默认图片代替
1.onerror 事件会在文档或图像加载过程中发生错误时被触发. 当图片不存在时,将触发onerror,onerror 中img为 指定的默认图片. 图片存在则显示正常图片,图片不存在将显示默认. ...
- vue 的watch用法
参考转自https://www.imooc.com/article/details/id/28187 类型:{ [key: string]: string | Function | Object | ...
- pod基本操作
目录 创建Pod 查询Pod 删除Pod 更新Pod @(kernetes虚拟化学习)[pod基本操作] pod基本操作 ---- 创建Pod kubectl create -f test_pod.y ...
- HandlerInterceptor拦截器使用总结
简介 SpringMVC的处理器拦截器,类似于Servlet开发中的过滤器Filter,用于对请求进行拦截和处理. 常见应用场景 1.权限检查:如检测请求是否具有登录权限,如果没有直接返回到登陆页面. ...
- 转换Excel格式
做一个功能需要将excel2003格式转成2007的格式,代码如下 需要引用office的microsoft.office.interop.excel.dll var app = new Micros ...
- python 缺失值的处理
- Unity 改变游戏对象的Scale引起的不好结果
1. 当一个游戏对象的中心(它的x,y,z的交点) 不在它的正中间时,改变它的Scale时它的中心也会变的. 如图:这是用PlayMkaer做的,胶囊体碰到瓶子瓶子会碎,会在瓶子的中心位置那生成一个 ...
- Murano Weekly Meeting 2016.08.09
Meeting time: 2016.August.09 1:00~2:00 Chairperson: Kirill Zaitsev, from Mirantis Meeting summary: ...
- LeetCode 367.有效的完全平方数(C++)
给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False. 说明:不要使用任何内置的库函数,如 sqrt. 示例 1: 输入:16 输出:True ...