阿里云 视频点播 签名机制 生成有问题 各位做过的 帮忙看看 指点一下谢谢 ...

[复制链接]
51 2
//获取 时间- (NSString *)dateTransformToTimeString{NSDate *currentDate = [NSDate date];//获得当前时间为UTC时间 2014-07-16 07:54:36 UTC (UTC时间比标准时间差8小时)//转为字符串NSDateFormatter*df = [[NSDateFormatter alloc]init];//实例化时间格式类[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//格式化//2014-07-16 07:54:36(NSString类)NSString *timeString = [df stringFromDate:currentDate];NSString *nian = [timeString substringToIndex:10];NSString *time = [timeString substringFromIndex:11];nian = [nian stringByAppendingString:@"T"];time = [time stringByAppendingString:@"Z"];timeString = [nian stringByAppendingString:time];timeString = [timeString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];return timeString;}//获取公共参数- (NSDictionary*)getSignature{NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:@"JSON",@"Format",@"2017-03-21",@"Version",@"LTAI5aqEw1sQV6uc",@"AccessKeyId",@"HMAC-SHA1",@"SignatureMethod",[self getUTF8NSString:[self dateTransformToTimeString]],@"Timestamp",@"1.0",@"SignatureVersion",[self uuidString],@"SignatureNonce",@"GetPlayInfo",@"Action",@"bfef6e65cf604959916e882f093fb549",@"VideoId",nil];return dic;}//特殊转义字符- (NSString *)getUTF8NSString:(NSString *)string{NSString *hString = [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@" /"":=*+"].invertedSet];return hString;}//获取uuid随机码- (NSString *)uuidString{CFUUIDRef uuid_ref = CFUUIDCreate(NULL);CFStringRef uuid_string_ref= CFUUIDCreateString(NULL, uuid_ref);NSString *uuid = [NSString stringWithString:(__bridge NSString *)uuid_string_ref];CFRelease(uuid_ref);CFRelease(uuid_string_ref);return [uuid lowercaseString];}//排序- (NSMutableDictionary*)stringWithDict:(NSDictionary*)dict{NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];for (NSString *str in [dict allKeys]){}NSArray *keys = [dict allKeys];NSArray *sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){return [obj1 compare:obj2 options:NSNumericSearch];}];for (int i = 0 ; i < sortedArray.count; i ++){[dic setObject:[dict objectForKey:[sortedArray objectAtIndex:i]] forKey:[sortedArray objectAtIndex:i]];}return dic;}- (NSArray*)stringWithKey:(NSDictionary*)dict{NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];for (NSString *str in [dict allKeys]){}NSArray *keys = [dict allKeys];NSArray *sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){return [obj1 compare:obj2 options:NSNumericSearch];}];return sortedArray;}//请求地址- (void)gethttp{NSString *str1 = @"";NSMutableDictionary *dics = [[self getSignature] mutableCopy];dics = [self stringWithDict:dics];NSLog(@"排序后 %@",dics);NSArray *array = [self stringWithKey:dics];for (int i = 0; i < array.count; i ++){NSString *str2 = [NSString stringWithFormat:@"%@=%@",[array objectAtIndex:i],dics[[array objectAtIndex:i]]];str1 = [str1 stringByAppendingString:[NSString stringWithFormat:@"&%@",str2]];}str1 = [@"%2F"stringByAppendingString:str1];str1 = [@"GET&"stringByAppendingString:str1];str1 = [self getUTF8NSString:str1];NSLog(@"数据 = %@",str1);NSString *str = [NSString stringWithFormat:@"http://vod.cn-shanghai.aliyuncs.com/?TimeStamp=%@&Format=JSON&AccessKeyId=LTAI5aqEw1sQV6uc&Action=GetVideoPlayAuth&SignatureMethod=HMAC-SHA1&SignatureNonce=%@&Version=2017-03-21&SignatureVersion=1.0&VideoId=bfef6e65cf604959916e882f093fb549&Signature=%@",[self dateTransformToTimeString],[self uuidString],[self getUTF8NSString:[self Base_HmacSha1:@"dLmr0ohAGzmpgHFjodrD4dJafEvPNp&" data:str1]]];//创建URLNSURL *url = [NSURL URLWithString:str];NSURLRequest *request = [NSURLRequest requestWithURL:url];//3、创建连接对象发送异步请求[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue new] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {//4、获取数据处理if (connectionError){NSLog(@"-----%@",connectionError);}else{NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];NSLog(@"打印数据 这事什么比婉莹%@",dic);}}];}//HmacSHA1加密-(NSString *)Base_HmacSha1:(NSString *)key data:(NSString *)data{const char *cKey = [key cStringUsingEncoding:NSASCIIStringEncoding];const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding];//Sha256:// unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];//CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);//sha1unsigned char cHMAC[CC_SHA1_DIGEST_LENGTH];CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), cData, strlen(cData), cHMAC);NSData *HMAC = [[NSData alloc] initWithBytes:cHMAClength:sizeof(cHMAC)];//将加密结果进行一次BASE64编码。NSString *hash = [HMAC base64EncodedStringWithOptions:0];// NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];NSLog(@"hmac = %@",hash);return hash;}


服务器最后返回的是:
{
Code = SignatureDoesNotMatch;
HostId = "vod.cn-shanghai.aliyuncs.com";
Message = "Specified signature is not matched with our calculation. server string to sign is:GET&%2F&AccessKeyId%3DLTAI5aqEw1sQV6uc%26Action%3DGetVideoPlayAuth%26Format%3DJSON%26SignatureMethod%3DHMAC-SHA1%26SignatureNonce%3D9fc32040-0ef3-486c-993c-a98f38f47175%26SignatureVersion%3D1.0%26TimeStamp%3D2018-05-07T14%253A08%253A01Z%26Version%3D2017-03-21%26VideoId%3Dbfef6e65cf604959916e882f093fb549";
Recommend = "https://error-center.aliyun.com/status/search?Keyword=SignatureDoesNotMatch&source=PopGw";
RequestId = "5AACC6FB-BEA2-425D-AB73-FC538E5EB79C";
}

举报 使用道具

回复

2 个评论

ericzh2003  新手上路  发表于 2019-3-7 15:08:04  | 显示全部楼层
//获取 时间- (NSString *)dateTransformToTimeString{NSDate *currentDate = [NSDate date];//获得当前时间为UTC时间 2014-07-16 07:54:36 UTC (UTC时间比标准时间差8小时)//转为字符串NSDateFormatter*df = [[NSDateFormatter alloc]init];//实例化时间格式类[df setDateFormat:@&quot;yyyy-MM-dd HH:mm:ss&quot;];//格式化//2014-07-16 07:54:36(NSString类)NSString *timeString = [df stringFromDate:currentDate];NSString *nian = [timeString substringToIndex:10];NSString *time = [timeString substringFromIndex:11];nian = [nian stringByAppendingString:@&quot;T&quot;];time = [time stringByAppendingString:@&quot;Z&quot;];timeString = [nian stringByAppendingString:time];timeString = [timeString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];return timeString;}//获取公共参数- (NSDictionary*)getSignature{NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:@&quot;JSON&quot;,@&quot;Format&quot;,@&quot;2017-03-21&quot;,@&quot;Version&quot;,@&quot;LTAI5aqEw1sQV6uc&quot;,@&quot;AccessKeyId&quot;,@&quot;HMAC-SHA1&quot;,@&quot;SignatureMethod&quot;,[self getUTF8NSString:[self dateTransformToTimeString]],@&quot;Timestamp&quot;,@&quot;1.0&quot;,@&quot;SignatureVersion&quot;,[self uuidString],@&quot;SignatureNonce&quot;,@&quot;GetPlayInfo&quot;,@&quot;Action&quot;,@&quot;bfef6e65cf604959916e882f093fb549&quot;,@&quot;VideoId&quot;,nil];return dic;}//特殊转义字符- (NSString *)getUTF8NSString:(NSString *)string{NSString *hString = [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@&quot; /&quot;&quot;:=*+&quot;].invertedSet];return hString;}//获取uuid随机码- (NSString *)uuidString{CFUUIDRef uuid_ref = CFUUIDCreate(NULL);CFStringRef uuid_string_ref= CFUUIDCreateString(NULL, uuid_ref);NSString *uuid = [NSString stringWithString:(__bridge NSString *)uuid_string_ref];CFRelease(uuid_ref);CFRelease(uuid_string_ref);return [uuid lowercaseString];}//排序- (NSMutableDictionary*)stringWithDict:(NSDictionary*)dict{NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];for (NSString *str in [dict allKeys]){}NSArray *keys = [dict allKeys];NSArray *sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){return [obj1 compare:obj2 options:NSNumericSearch];}];for (int i = 0 ; i < sortedArray.count; i ++){[dic setObject:[dict objectForKey:[sortedArray objectAtIndex:i]] forKey:[sortedArray objectAtIndex:i]];}return dic;}- (NSArray*)stringWithKey:(NSDictionary*)dict{NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];for (NSString *str in [dict allKeys]){}NSArray *keys = [dict allKeys];NSArray *sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){return [obj1 compare:obj2 options:NSNumericSearch];}];return sortedArray;}//请求地址- (void)gethttp{NSString *str1 = @&quot;&quot;;NSMutableDictionary *dics = [[self getSignature] mutableCopy];dics = [self stringWithDict:dics];NSLog(@&quot;排序后 %@&quot;,dics);NSArray *array = [self stringWithKey:dics];for (int i = 0; i < array.count; i ++){NSString *str2 = [NSString stringWithFormat:@&quot;%@=%@&quot;,[array objectAtIndex:i],dics[[array objectAtIndex:i]]];str1 = [str1 stringByAppendingString:[NSString stringWithFormat:@&quot;&%@&quot;,str2]];}str1 = [@&quot;%2F&quot; stringByAppendingString:str1];str1 = [@&quot;GET&&quot; stringByAppendingString:str1];str1 = [self getUTF8NSString:str1];NSLog(@&quot;数据 = %@&quot;,str1);NSString *str = [NSString stringWithFormat:@&quot;http://vod.cn-shanghai.aliyuncs.com/?TimeStamp=%@&Format=JSON&AccessKeyId=LTAI5aqEw1sQV6uc&Action=GetVideoPlayAuth&SignatureMethod=HMAC-SHA1&SignatureNonce=%@&Version=2017-03-21&SignatureVersion=1.0&VideoId=bfef6e65cf604959916e882f093fb549&Signature=%@&quot;,[self dateTransformToTimeString],[self uuidString],[self getUTF8NSString:[self Base_HmacSha1:@&quot;dLmr0ohAGzmpgHFjodrD4dJafEvPNp&&quot; data:str1]]];//创建URLNSURL *url = [NSURL URLWithString:str];NSURLRequest *request = [NSURLRequest requestWithURL:url];//3、创建连接对象发送异步请求[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue new] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {//4、获取数据处理if (connectionError){NSLog(@&quot;-----%@&quot;,connectionError);}else{NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];NSLog(@&quot;打印数据 这事什么比婉莹%@&quot;,dic);}}];}//HmacSHA1加密-(NSString *)Base_HmacSha1:(NSString *)key data:(NSString *)data{const char *cKey = [key cStringUsingEncoding:NSASCIIStringEncoding];const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding];//Sha256:// unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];//CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);//sha1unsigned char cHMAC[CC_SHA1_DIGEST_LENGTH];CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), cData, strlen(cData), cHMAC);NSData *HMAC = [[NSData alloc] initWithBytes:cHMAClength:sizeof(cHMAC)];//将加密结果进行一次BASE64编码。NSString *hash = [HMAC base64EncodedStringWithOptions:0];// NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];NSLog(@&quot;hmac = %@&quot;,hash);return hash;}


服务器最后返回的是:
{
Code = SignatureDoesNotMatch;
HostId = &quot;vod.cn-shanghai.aliyuncs.com&quot;;
Message = &quot;Specified signature is not matched with our calculation. server string to sign is:GET&%2F&AccessKeyId%3DLTAI5aqEw1sQV6uc%26Action%3DGetVideoPlayAuth%26Format%3DJSON%26SignatureMethod%3DHMAC-SHA1%26SignatureNonce%3D9fc32040-0ef3-486c-993c-a98f38f47175%26SignatureVersion%3D1.0%26TimeStamp%3D2018-05-07T14%253A08%253A01Z%26Version%3D2017-03-21%26VideoId%3Dbfef6e65cf604959916e882f093fb549&quot;;
Recommend = &quot;https://error-center.aliyun.com/status/search?Keyword=SignatureDoesNotMatch&source=PopGw&quot;;
RequestId = &quot;5AACC6FB-BEA2-425D-AB73-FC538E5EB79C&quot;;
}

举报 使用道具

回复
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

ios.irunthink.com
搜索
热搜: iOS swift 下拉刷新

图文热点

更多

开发教程

更多

客服中心

400-800 周一至周日 9:30-17:30 仅收市话费

关注我们

  • 微信扫描
  • 关注微信
关于我们
关于我们
友情链接
联系我们
帮助中心
精贴推荐
源码上传
注册登录
服务支持
资源下载
关注我们
官方微博
官方空间
快速回复 返回顶部 返回列表