融云使用之获取Token值

作者: 会走路的键盘 | 来源:发表于2016-07-14 11:19 被阅读1259次

    先看一下官方给出的API签名规则


    融云.png

    然后是签名请求参数准备,新建一个MSTool类,并导入头文件#import <CommonCrypto/CommonCrypto.h>

    代码如下:

    #import "MSTool.h"
    #import <CommonCrypto/CommonCrypto.h>
    
    @implementation MSTool
    
    + (instancetype)sharedMSTool{
        static MSTool * tool = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            tool = [[self alloc] init];
        });
        return tool;
    }
    
    //single_implementation(MSTool)
    
    //获取随机数
    -(NSString *)getRandomNonce
    {
        NSInteger randomValue = [self getRandomNumber:100000 to:999999];
        return  [NSString stringWithFormat:@"%ld",randomValue];
    }
    //获取时间戳 从1970年
    -(NSString *)getTimestamp
    {
        NSDate *date = [NSDate date];
        NSTimeInterval times =  [date timeIntervalSince1970];
        return [NSString stringWithFormat:@"%.0f",times];
    }
    
    //获取从 from 到  to 的随机数
    -(NSInteger)getRandomNumber:(NSInteger)from to:(NSInteger)to
    {
        return (NSInteger)(from + (arc4random() % (to - from + 1)));
    }
    
    //sha1 加密
    -(NSString *)sha1WithKey:(NSString *)key
    {
        const char *cstr = [key cStringUsingEncoding:NSUTF8StringEncoding];
        NSData *data = [NSData dataWithBytes:cstr length:key.length];
        
        uint8_t digest[CC_SHA1_DIGEST_LENGTH];
        
        CC_SHA1(data.bytes, data.length, digest);
        
        NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
        
        for(int i=0; i<CC_SHA1_DIGEST_LENGTH; i++) {
            [output appendFormat:@"%02x", digest[i]];
        }
        
        return output;
    }
    
    //根据appSecret nonce timestamp 获取signature
    -(NSString *)getSignatureWithAppSecret:(NSString *)appSecret nonce:(NSString *)nonce timestamp:(NSString *)timestamp
    {
        NSString *sha1String = [NSString stringWithFormat:@"%@%@%@",appSecret,nonce,timestamp];
        return [self sha1WithKey:sha1String];
    }
    @end
    

    然后是在Appdelegate中设置

    #pragma mark - 融云客服设置
    - (void)getToken{
        
        NSString * url = @"https://api.cn.ronghub.com/user/getToken.json";
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.requestSerializer  = [AFHTTPRequestSerializer serializer];
        
        NSDictionary *dict = @{@"userId":请求用户唯一标识符@"name":用户名,@"portraiUri":图片的网址"};
        
        NSString *appkey = @"你在融云申请的App Key";
        NSString *nonce = [[MSTool sharedMSTool] getRandomNonce];
        NSString *timestamp = [[MSTool sharedMSTool] getTimestamp];
        NSString *signature = [[MSTool  sharedMSTool]getSignatureWithAppSecret:@"你在融云申请的AppSecret" nonce:nonce timestamp:timestamp];
    
        //设置请求头
        [manager.requestSerializer setValue:appkey forHTTPHeaderField:@"App-Key"];
        [manager.requestSerializer setValue:nonce forHTTPHeaderField:@"Nonce"];
        [manager.requestSerializer setValue:timestamp forHTTPHeaderField:@"Timestamp"];
        [manager.requestSerializer setValue:signature forHTTPHeaderField:@"Signature"];
        //调用POST方法
        [manager POST:url parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"😪%@",responseObject);
          
            //客服设置
            [[RCIM sharedRCIM]initWithAppKey:@"你在融云申请的App Key"];
            [[RCIM sharedRCIM] connectWithToken:responseObject[@"token"] success:^(NSString *userId) {
                NSLog(@"LoginSuccess");
            } error:^(RCConnectErrorCode status) {
                NSLog(@"LoginFault");
            } tokenIncorrect:^{
                NSLog(@"Token_isEorro");
            }];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%@",error);
        }];
    }
    

    相关文章

      网友评论

        本文标题:融云使用之获取Token值

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