zoukankan      html  css  js  c++  java
  • OAuth授权之回调accessToken

    具体说明见新浪官方文档
    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"

     }

  • 相关阅读:
    Mysql 命令行连接
    linux下安装MongoDB数据库
    SVN 提交常见报错及解决方案
    解决 SVN Skipped 'xxx' -- Node remains in conflict
    linux svn 切换用户
    SQL基础语法
    yml
    搭建笔记(1)
    文件上传MultipartFile
    18.线程池
  • 原文地址:https://www.cnblogs.com/MengXY/p/4110043.html
Copyright © 2011-2022 走看看