美文网首页
IOS网络篇------微博三方登录

IOS网络篇------微博三方登录

作者: 包佳奇 | 来源:发表于2017-04-09 16:36 被阅读163次

    大家在使用一款新的软件时都会让注册,不过你嫌账号太多麻烦也可以点击下面的第三方登录,例如微博、微信、QQ、人人等等.以微博三方登录为例,利用微博开放平台暴露的接口来实现一下微博的三方登录

    1.首先打开网页登录http://open.weibo.com

    2.接下来是在文档中找到对应的授权接口


    1.png
    2.png
    3.png
    4.png

    注:
    ①.这个请求的的地址不能拿AF来请求,只能是通过webView加载
    ②.为了避免三方登陆接触到自身的用户名和密码,通过网页解决这个问题,所以更安全,登录成功信息通过回调查看是否登陆成功

    3.请求参数找必选的(client_id, redirect_uri)
    注:地址与参数之间用?连接参数与参数之间用&
    eg:https://api.weibo.com/oauth2/authorize?client_id=&redirect_uri=http://www.xxxxx.com

    4.在我的应用中创建应用,提取App Key和App Secret
    eg:
    App Key:123456789
    App Secret:a6a6e8e254c3c13372fd6b556c1b4837
    https://api.weibo.com/oauth2/authorize?client_id=123456789&redirect_uri=http://www.xxxxx.com

    5.通过网页加载url进行测试
    授权错误


    5.png

    解决办法:


    6.png 请求正确:
    7.png

    6.实现webView代理协议,在开始加载时查看请求URL的的详细信息

    #import "LoginViewController.h"
    #import "WeiboTool.h"
    
    #define AccountPath  [NSHomeDirectory() stringByAppendingString:@"Documents/Account.plist"]
    @interface LoginViewController ()<UIWebViewDelegate>
    @end
    
    @implementation LoginViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        UIWebView *wv = [[UIWebView alloc]initWithFrame:self.view.bounds];
        wv.delegate = self;
        [self.view addSubview:wv];
        
        NSURL *url = [NSURL URLWithString:@"https://api.weibo.com/oauth2/authorize?client_id=123456789&redirect_uri=http://www.xxxxx.com"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [wv loadRequest:request];
        
    }
    //拦截URL请求,拦截带code的URL
    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
        
        NSString *path = [request.URL description];
       
        NSLog(@"-------%@",path);
        //调用该方法相当于调用rangeOfString看看里面是否有code
        if ([path containsString:@"code"]) {
            NSString *code = [[path componentsSeparatedByString:@"="]lastObject];
            [WeiboTool requestTokenWithCode:code andWithBlock:^(id obj) {
                
                NSDictionary *dic = obj;
                //用户登录后一次后不再进行登录
                [dic writeToFile:AccountPath atomically:YES];
                NSLog(@"登录成功!");
                [self dismissViewControllerAnimated:YES completion:nil];
            }];
            return NO;
        }
        return YES;
    }
    
    @end
    
    

    7.创建工具类利用AF进行请求


    8.png

    WeiboTool.h

    #import <Foundation/Foundation.h>
    #import "AFNetworking.h"
    
    @interface WeiboTool : NSObject
    
    + (void)requestTokenWithCode:(NSString *)code andWithBlock:(void(^)(id obj))datablock;
    
    @end
    
    

    WeiboTool.m

    #import "WeiboTool.h"
    
    @implementation WeiboTool
    
    +(void)requestTokenWithCode:(NSString *)code andWithBlock:(void (^)(id obj))datablock {
        NSString *path = @"https://api.weibo.com/oauth2/access_token";
        NSDictionary *params = @{@"client_id":@"123456789",
                                 @"client_secret":@"a6a6e8e254c3c13372fd6b556c1b4837",
                                 @"grant_type":@"authorization_code",
                                 @"redirect_uri":@"http://www.baidu.com",
                                 @"code":code};
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        [manager POST:path parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
            NSLog(@"%@",dic);
            datablock(dic);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"请求失败:%@",error);
        }];
        
    }
    
    @end
    

    8.用code换token(令牌)最终在日志显示返回数据和文档一致

    网盘下载链接:链接: https://pan.baidu.com/s/1jHNfvnw 密码: cydi

    相关文章

      网友评论

          本文标题:IOS网络篇------微博三方登录

          本文链接:https://www.haomeiwen.com/subject/atvdattx.html