在 iOS 中与 REST Api 进行会话时出现 QuickBlox“意外签名”错误

Posted

技术标签:

【中文标题】在 iOS 中与 REST Api 进行会话时出现 QuickBlox“意外签名”错误【英文标题】:QuickBlox "Unexpected Signature" error while making session with REST Api in iOS 【发布时间】:2014-07-05 11:47:16 【问题描述】:

这可能是一个重复的问题,但我找不到任何关于它有什么问题的信息,我一直在尝试通过 QuickBlox 的 rest API 生成一个会话,它已经消耗了 5 天,但我无法通过它。请帮帮我

设置身体

NSString *strNonceValue = [NSString stringWithFormat:@"%d", arc4random() % 1000000];
    NSString *timeStampValue = [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970]];

NSMutableDictionary *dictSessionInfo = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                            applicationID, @"application_id",
                                            _pAuthorizationKey, @"auth_key",
                                            timeStampValue, @"timestamp",
                                            strNonceValue, @"nonce", nil];

NSString *signature = [self generateSignatureWithText:dataVal andKey:_pAuthorizationKey];

[dictSessionInfo setObject:signature forKey:@"signature"];

NSData *data = [NSJSONSerialization dataWithJSONObject:dictSessionInfo options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

获取签名

- (NSString *)generateSignatureWithText:(NSData *)data andKey:(NSString *)secret 

    NSData *secretData = [secret dataUsingEncoding:NSUTF8StringEncoding];
    NSData *clearTextData = data;
    uint8_t digest[CC_SHA1_DIGEST_LENGTH] = 0;
    CCHmacContext hmacContext;
    CCHmacInit(&hmacContext, kCCHmacAlgSHA1, secretData.bytes, secretData.length);
    CCHmacUpdate(&hmacContext, clearTextData.bytes, clearTextData.length);
    CCHmacFinal(&hmacContext, digest);
    NSData *result = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
    NSString *hash = [result description];
    hash = [hash stringByReplacingOccurrencesOfString:@" " withString:@""];
    hash = [hash stringByReplacingOccurrencesOfString:@"<" withString:@""];
    hash = [hash stringByReplacingOccurrencesOfString:@">" withString:@""];

    return hash;   

生成 URLRequest

requestURL = [requestURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:requestURL];
NSString *postLength=@"";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setTimeoutInterval:60];
[request setURL:url];
NSLog(@"HTTP body Fields : %@", combinedDataStr);
if([requestType isEqualToString:@"POST"]) 
    NSData *postData = [combinedDataStr dataUsingEncoding:NSASCIIStringEncoding
                                     allowLossyConversion:YES];
    postLength = [NSString stringWithFormat:@"%ld", (unsigned long)[postData length]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:postData];

else
    [request setHTTPMethod:@"GET"];

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"0.1.0"            forHTTPHeaderField:@"QuickBlox-REST-API-Version"];

我收到的回复是:

response dict : 
    errors =     
        base =         (
            "Unexpected signature"
        );
    ;

请调查一下,让我知道我做错了什么

【问题讨论】:

您的问题解决了吗?如果是,那么您可以发布解决方案,因为我遇到了同样的问题。 【参考方案1】:

我遇到了同样的问题。我已尝试解决此问题,请尝试以下步骤

    卸载 pod 'QuickBlox'

    重新安装 Quickblox pod 文件,它将使用最新的框架和重建项目,

你会看到这个问题的魔力解决了:) 请让我知道上述步骤对您有用吗?

谢谢

【讨论】:

【参考方案2】:

据我了解,问题出在这一行

NSString *signature = [self generateSignatureWithText:dataVal andKey:_pAuthorizationKey];

您使用身份验证密钥创建签名,但您应该在此处传递密钥

所以,尝试用密钥替换 _pAuthorizationKey

【讨论】:

谢谢@Igor Khomenko,我会测试它并很快在这里更新 我刚刚发现您是 quickblox 的开发人员。我正在尝试在我的应用中实现视频聊天。但无法获得有效的签名。你能帮我如何获得有效的签名。在这个问题中,我无法理解在方法中传递的dataval 是什么。如果可能,请帮助 @Igor dataVal 是什么?这里需要做什么?

以上是关于在 iOS 中与 REST Api 进行会话时出现 QuickBlox“意外签名”错误的主要内容,如果未能解决你的问题,请参考以下文章