这可能是一个重复的问题,但我无法找到有关它的错误的任何内容,我一直在尝试通过QuickBlox的休息API生成一个会话,它耗费了5天但我可以& #39; t得到它。请帮帮我
设置身体
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"
);
};
}
请仔细研究,让我知道我做错了什么
答案 0 :(得分:1)
我遇到了同样的问题。我已尝试解决此问题,请尝试以下步骤
卸载
pod'QuickBlox'
重新安装Quickblox pod文件,它将使用最新框架和重建项目,
您将看到解决此问题的神奇方法:) 请让我知道以上步骤对您有用吗?
谢谢
答案 1 :(得分:0)
据我了解,问题在于这一行
NSString *signature = [self generateSignatureWithText:dataVal andKey:_pAuthorizationKey];
您使用auth密钥创建签名,但您应该在此处传递密钥
所以,尝试用密钥替换_pAuthorizationKey