第三方微博登陆,通过OAuth 2.0 进行授权

  第一步 去新浪申请应用,设置回调地址   // 代码详见 https://github.com/xiaoqib/ChenWeiboDemo

申请到 App Key:3123440635  和 App Secret:ab212d5577205050de55a0f2c41d8477  这是我申请的,每个人申请的不一样。

然后设置回调的地址  https://api.weibo.com/oauth2/default.html  我用的是这个,还可以是百度的,下图是设置回调地址的页面。

  第二步  获得code 

在ViewController.h 中 

@interface ViewController : UIViewController<UIWebViewDelegate>

然后在ViewController.m 中   创建一个webView的实例,然后调用代理方法。  在viewWillAppear 中 写请求,其中包含client_id 就是   App Key: 3123440635 redirect_uri 是回调地址,一定要和之前设置的一致。  response_type=code  是响应的类型是code  类型,这个返回的code就是我们先需要的 。 display=mobile 是指在移动端实现。下面代码中红色的部分,即为返回得到的一个url 字符串,要通过截取,才能得到code值。 

</pre><pre name="code" class="objc">#import "ViewController.h"
#import "Masonry.h"

@interface ViewController ()
@property (nonatomic,strong) UIWebView *webView;

@end

@implementation ViewController


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    NSString *url = @"https://api.weibo.com/oauth2/authorize?client_id=3123440635&redirect_uri=https://api.weibo.com/oauth2/default.html&response_type=code&display=mobile;";
    
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:url]];
    
    [self.webView setDelegate:self];
    [self.webView loadRequest:request];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//    LandingView *landingView = [[LandingView alloc]initWithFrame:self.view.frame];;
//    
//    [self.view addSubview:landingView];
    self.webView = [[UIWebView alloc]init];
    
    [self.view addSubview:self.webView];
    
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.equalTo(self.view.mas_top);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.bottom.equalTo(self.view.mas_bottom);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UIWebView的代理方法

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    NSLog(@"222222222");
    
    <span style="color:#ff0000;">NSString *url = webView.request.URL.absoluteString;
    NSLog(@"absolutestring:%@",url);</span>
    
    //找到" code= " 的range
    NSRange rangeOne;
    rangeOne = [url rangeOfString:@"="];
    
    //获取code值
    NSString * codeString = [url substringFromIndex:rangeOne.location+1];
    NSLog(@"code = :%@",codeString);
    
    //access token 调用URL的string
    NSMutableString *muString = [[NSMutableString alloc]initWithString:@"https://api.weibo.com/oauth2/access_token?client_id=3123440635&client_secret=ab212d5577205050de55a0f2c41d8477&grant_type=authorization_code&redirect_uri=https://api.weibo.com/oauth2/default.html&code="];
    [muString appendString:codeString];
    NSLog(@"access token url :%@",muString);
        
    //第一步,创建URL
    NSURL *urlString = [NSURL URLWithString:muString];
    //第二部,创建请求
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:urlString cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
    [request setHTTPMethod:@"POST"];
    NSString *str = @"type=focus-c";
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    //第三步,连接服务器
    NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *str1 = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding];
        
    NSLog(@"Back String :%@",str1);
        
    NSError *error;
    //如何从str1中获取到access_token
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:received options:NSJSONReadingMutableContainers error:&error];
        
    NSString *_access_token = [dictionary objectForKey:@"access_token"];
        
    NSLog(@"access token is:%@",_access_token);

}

第三步 将截取到的code值 转化成 token 

由上面代码,可以看到 截取到code=  后面的值以,通过一系列方法, 最终得到 所需要的access_token ,由这个token就可以去调用 API 接口。 

     

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值