使用HTTP Post iOS / Android将数据发送到Azure Event Hub

时间:2016-07-28 19:20:09

标签: android ios azure authorization azure-eventhub

我一直在尝试使用iOS应用程序中的HTTP post将数据发布到Azure Event Hub,但我收到“ 401 40103:无效的授权令牌签名”错误。我已按照here提到的完全相同的步骤生成共享访问签名令牌。这是我生成令牌

的代码
- (NSString *)createTokenFromResourceUri:(NSString *)resourceUri keyName:(NSString *)keyName key:(NSString *)key {
    NSInteger expiry = [[NSDate date] timeIntervalSince1970];
    expiry = expiry + 3600;      //Adding 1 hour

    NSString *encodedUrl = [resourceUri stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSString *stringToSign = [NSString stringWithFormat:@"%@\n%ld", encodedUrl, (long)expiry];

    const char *cKey  = [key cStringUsingEncoding:NSASCIIStringEncoding];
    const char *cData = [stringToSign cStringUsingEncoding:NSASCIIStringEncoding];

    unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];

    CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);

    NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];
    NSString *hash = [HMAC base64EncodedStringWithOptions:0];

    return [NSString stringWithFormat:@"SharedAccessSignature sig=%@&se=%ld&skn=%@&sr=%@", hash, (long)expiry, keyName, encodedUrl];
}

然后我使用http客户端发布数据

self.httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:uri]];
[self.httpClient setDefaultHeader:@"Authorization" value:[self createTokenFromResourceUri:uri keyName:keyName key:key]];
[self.httpClient postPath:nil parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {

} failure:^(AFHTTPRequestOperation operation, NSError error) {
}];

我也在编写一个Android客户端并在那里得到同样的错误。

我做错了什么?

0 个答案:

没有答案