Reputation: 1028
This might be a repeated question but I can't find anything about what is wrong about it, I have been trying to generate a session via QuickBlox's rest API, it has consumed 5 days but I can't get it through. Please help me out
Setting the body
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];
Getting Signature
- (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;
}
Generating 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"];
The response I am receiving is :
response dict : {
errors = {
base = (
"Unexpected signature"
);
};
}
Please look into this and let me know what I am doing wrong
Upvotes: 0
Views: 1179
Reputation: 1192
Same issue I have faced. I have tried to solve this issue please try below step
Uninstall
pod 'QuickBlox'
Reinstall Quickblox pod file it will take Latest framework and Rebuild projects,
You will see the magic of this issue by solved :) Please let me know above steps are worked for you?
Thanks
Upvotes: 1
Reputation: 18346
As far as I understand, the problem is with this line
NSString *signature = [self generateSignatureWithText:dataVal andKey:_pAuthorizationKey];
you use auth key to create a signature, but you should pass here secret key
so, try to replace _pAuthorizationKey with secret key
Upvotes: 0