我正在从 iCloud 获取数据,为此我需要生成一个标头(天蓝色表存储)。
我为此使用了下面的代码,它正在生成标题。但是当我在我的项目中使用这些标头时,它显示“确保授权标头的值正确形成,包括签名。”
我用谷歌搜索了很多代码并尝试了很多代码,但都是徒劳的。
任何人都可以请帮我解决我在这段代码中出错的地方。
-(id)generat
{
NSString *messageToSign = [NSString stringWithFormat:@"%@/%@/%@", dateString,AZURE_ACCOUNT_NAME, tableName];
NSString *key = @"asasasasasasasasasasasasasasasasasasasasas==";
const char *cKey = [key cStringUsingEncoding:NSUTF8StringEncoding];
const char *cData = [messageToSign cStringUsingEncoding:NSUTF8StringEncoding];
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 = [Base64 encode:HMAC];
NSLog(@"Encoded hash: %@", hash);
NSURL *url=[NSURL URLWithString: @"http://my url"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request addValue:[NSString stringWithFormat:@"SharedKeyLite %@:%@",AZURE_ACCOUNT_NAME, hash] forHTTPHeaderField:@"Authorization"];
[request addValue:dateString forHTTPHeaderField:@"x-ms-date"];
[request addValue:@"application/atom+xml, application/xml"forHTTPHeaderField:@"Accept"];
[request addValue:@"UTF-8" forHTTPHeaderField:@"Accept-Charset"];
NSLog(@"Headers: %@", [request allHTTPHeaderFields]);
NSLog(@"URL: %@", [[request URL] absoluteString]);
return request;
}
-(NSString*)rfc1123String:(NSDate *)date
{
static NSDateFormatter *df = nil;
if(df == nil)
{
df = [[NSDateFormatter alloc] init];
df.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
df.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'";
}
return [df stringFromDate:date];
}