iOS开发--大众点评API请求签名生成代码示例

参考文档:http://developer.dianping.com/app/documentation/signature


#import <CommonCrypto/CommonDigest.h>

+ (NSDictionary *)parseQueryString:(NSString *)query {
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:6];
    NSArray *pairs = [query componentsSeparatedByString:@"&"];
    
    for (NSString *pair in pairs) {
        NSArray *elements = [pair componentsSeparatedByString:@"="];
        
        if ([elements count] <= 1) {
            return nil;
        }
        
        NSString *key = [[elements objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSString *val = [[elements objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        [dict setObject:val forKey:key];
    }
    return dict;
}

//签名算法如下:
//1. 对除appkey以外的所有请求参数进行字典升序排列;
//2. 将以上排序后的参数表进行字符串连接,如key1value1key2value2key3value3...keyNvalueN;
//3. 将app key作为前缀,将app secret作为后缀,对该字符串进行SHA-1计算,并转换成16进制编码;
//4. 转换为全大写形式后即获得签名串
+ (NSString *)generateDianpingUrlWithLatitude:(double)latitude longtitude:(double)longitude
{
    NSString *url = nil;
    NSString *appKey = @"842135602";
    NSString *appSecret = @"e3f1d94fe7764d8a9efb5dae2abeceac";
    NSMutableString *baseUrl = [NSMutableString stringWithFormat:@"http://api.dianping.com/v1/business/find_businesses?latitude=%f&longitude=%f&sort=7", latitude, longitude];
    //前缀
    NSMutableString *signString = [NSMutableString stringWithString:appKey];
    //中间的key1value1key2value2key3value3...keyNvalueN
    NSURL* parsedURL = [NSURL URLWithString:[baseUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSMutableDictionary *paramsDic = [NSMutableDictionary dictionaryWithDictionary:[self parseQueryString:[parsedURL query]]];
    NSArray *sortedKeys = [[paramsDic allKeys] sortedArrayUsingSelector: @selector(compare:)];
    for (NSString *key in sortedKeys) {
        [signString appendFormat:@"%@%@", key, [paramsDic objectForKey:key]];
    }
    //后缀
    [signString appendString:appSecret];
    
    //对该字符串进行SHA-1计算,并转换成16进制编码;
    unsigned char digest[CC_SHA1_DIGEST_LENGTH];
    NSData *stringBytes = [signString dataUsingEncoding: NSUTF8StringEncoding];
    if (CC_SHA1([stringBytes bytes], [stringBytes length], digest)) {
        NSMutableString *digestString = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH];
        for (int i=0; i<CC_SHA1_DIGEST_LENGTH; i++) {
            unsigned char aChar = digest[i];
            [digestString appendFormat:@"%02X", aChar];
        }
        url = [[NSString alloc] initWithFormat:@"%@&appkey=%@&sign=%@", baseUrl, appKey, [digestString uppercaseString]]; //转换为全大写形式后即获得签名串
    }
    return url;
}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。