3

我正在从 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];
}
4

1 回答 1

0

看起来您可能缺少换行符:

NSString *messageToSign = [NSString stringWithFormat:@"%@/%@/%@", dateString,AZURE_ACCOUNT_NAME, tableName];

我不知道Objective-C,但我希望格式是@"%@\n/%@/%@"(如果这就是字符串文字中的换行符在这种编程语言中的工作方式)。

另外,我认为您将需要这两个标题:

DataServiceVersion:1.0;NetFx
MaxDataServiceVersion:1.0;NetFx

编辑:在我看来,这些标头可能出现在您代码的另一部分中。您可能还需要x-ms-version标头(也可能出现在代码的其他地方)。/编辑

我不能凭良心指出将存储密钥放在客户端(如电话)上是个坏主意。任何获得您的应用程序的人都可以提取密钥并使用它来删除您的所有数据、上传他们的私人 DVD 收藏等。您的存储密钥是一个秘密,因此永远不应该分发给客户。

于 2012-05-30T16:38:30.947 回答