Log in or register to post comments

getting Error to access the VWS API

February 18, 2013 - 4:06am #1

 

I am trying to access the VWS from objective-C.Please help me in this regard.I am getting an error indicating below.The sample is on java.I will appreciate if u will provide us the sample of VWS on objective-C too.

 

Error:

<html><head><title>Apache Tomcat/6.0.35 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 404 - </h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>The requested resource () is not available.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/6.0.35</h3></body></html>

getting Error to access the VWS API

December 6, 2015 - 4:35am #9

DId you guys ever find a solution here? 

 

I've got similar code to above, however I am trying with a GET approach, and therefore remove an chance it is the objective-c MD5 algorithm. As such, I believe that this must be due to the HMAC routine? 

 

If there is anyone on this forum, that knows enough objective-c to validate the signing approach here, that would be of huge assistance. My code is as follows:

The below returns a 401


- (void) getCloudTargetSummary{
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    df.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'";
    NSString *date = [df stringFromDate:[NSDate date]];
    
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", urlSource, @"/summary"]];
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    request.HTTPMethod = @"GET";
    [request addValue:date forHTTPHeaderField:@"Date"];
    NSString *signature = [self buildSignature: request : secretKey : date];
    
    NSString *signingString = [NSString stringWithFormat:@"VWS %@:%@",accessKey, signature];
    NSLog(@"Signing Key = %@",signingString);
    [request setValue:signingString forHTTPHeaderField:@"Authorization"];
    
    
    NSURLSessionDataTask *getTask = [session dataTaskWithRequest:request
                                                    completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                        
                                                        NSLog(@"Response %@", response);
                                                        NSLog(@"error %@", error);
                                                        
                                                        NSDictionary * json  = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
                                                        NSLog(@"%@",json);
                                                        
                                                        
                                                    }];
    
    [getTask resume];
    
}

- (NSString *) buildSignature : (NSMutableURLRequest *) request : (NSString *) secretKey : (NSString *) date{
    
    NSString *method = [request HTTPMethod];
    NSString *contentType = @"";
    NSString *hexDigest = @"d41d8cd98f00b204e9800998ecf8427e"; // Hex digest of an empty string
    
    if ([method isEqualToString:@"GET"] || [method isEqualToString: @"DELETE"]) {
        // Do nothing because the strings are already set correctly
        hexDigest = @"d41d8cd98f00b204e9800998ecf8427e";
    } else if( [method isEqualToString:@"POST"] || [method isEqualToString:@"PUT"]) {
        contentType = @"application/json";
        NSString *bodyString =  [[NSString alloc] initWithData:[request HTTPBody] encoding:NSASCIIStringEncoding];
        hexDigest = [self md5HexDigest:bodyString];
        NSLog(@"hexDigest%@", hexDigest);
        
    } else {
        NSLog(@"ERROR: Invalid content type passed to Sig Builder");
    }
    
    NSString *toDigest = [NSString stringWithFormat:@"%@\n%@\n%@\n%@\n%@",method,hexDigest,contentType,date, [request URL].path];
    NSLog(@"To Digest: %@", toDigest);
    return [self getBase64HMAC:toDigest withKey:secretKey];
   // return [self HMAC:secretKey :toDigest];
    
}

// NOT BEING USED FOR GET
- (NSString*)md5HexDigest:(NSString*)input {
    const char* str = [input UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(str, (int)strlen(str), result);
    
    NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH*2];
    for(int i = 0; i<CC_MD5_DIGEST_LENGTH; i++) {
        [ret appendFormat:@"%02x",result[i]];
    }
    return ret;
}

- (NSString*)getBase64HMAC:(NSString*)data withKey:(NSString*)secret
{

    const char *cKey  = [secret cStringUsingEncoding:NSASCIIStringEncoding];
    const char *cData = [data 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)];
    
    // Get NSString from NSData object in Base64
    NSString *base64Encoded = [HMAC base64EncodedStringWithOptions:0];
    
    return base64Encoded;
}

 

getting Error to access the VWS API

March 13, 2013 - 10:07am #8

 

Hi nikhilbansals42,

 

I have been trying to do Objective C way, but no luck so far.

If you have any progress on your project and share it, it would be so greateful. 

Thanks!!

 

getting Error to access the VWS API

February 19, 2013 - 5:07am #7

Hi,

Thanks for your kind reply.

Yes,they got success using C#.There are so many options to do so :).Right now I am focusing on Objective-C.

but you are right,I can use the java samples too.

I will get back to you if I face anyother problem.

once again thanks for your reply.

 

getting Error to access the VWS API

February 19, 2013 - 4:47am #6

Hi,

We don't have a date for the PHP samples.

I think someone in the forums had success doing this from a Unity script:

https://developer.vuforia.com/forum/cloud-recognition/vws-requests-c-unity-authentication

I am trying to do in Objective-C,

Yes I know, but do you want to upload the images once only?  If so, have you tried the java samples? as they are fairly easy to get working once you download Eclipse...

N

getting Error to access the VWS API

February 19, 2013 - 4:32am #5

Hi,

I am trying to do in Objective-C, hope I will find the solution.

I have read in the forum that you guys are working on VWS php sample.So when are you planing to release php samples for VWS.

getting Error to access the VWS API

February 19, 2013 - 3:44am #4

Hi,

At the moment we do not have any examples of calling the VWS api from Objective-C because initially the aim was to enable loading of images from servers - hence the Java samples as a starting point.

Can I just ask what is the use-case you are looking to implement here?  Are you trying to implement upload from the device?  

If so then the recommended way of doing this is to upload to your own server which will then act as a gateway to the Cloud Database.

 

N

getting Error to access the VWS API

February 19, 2013 - 1:19am #3

Following is the code:

 

 

- (IBAction)postBtnTapped:(id)sender {

    NSString *accessKey = @"SERVER_ACCESS_KEY";

    NSString *secretKey=@"SERVER_SECRET_KEY";

    UIImage *img =[[UIImage allocinitWithContentsOfFile:@"stones.png"];

    NSData *data = UIImagePNGRepresentation(img);

    NSString *imageDataString = [data base64Encoding];    //// I have used the Category to find out the base64encoded String

    NSString *imgWidth = [[NSNumber numberWithFloat:img.size.widthstringValue];

    NSDictionary * postDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"ASW_Image",@"name", img.size.width,@"width" , imageDataString,@"image"nil];

    NSError * error = nil;

    NSData * jsonData = [NSJSONSerialization dataWithJSONObject:postDictionary options:NSJSONReadingMutableContainers error:&error];

    

    NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://vws.vuforia.com/targets"]];

     [urlRequest setHTTPMethod:@"POST"];

    [urlRequest setHTTPBody:jsonData];

    

     NSDate *currDate = [NSDate date];

    NSString *dateStr = [self getUTCFormateDate:currDate];

    NSLog(@"DateFormat: %@",dateStr);

    [urlRequest setValue: dateStr forHTTPHeaderField:@"Date"];

    [urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

   NSString *contentmd5 =@"I am having trouble to find this String in Objective-C"

    NSString *dataValue =[NSString stringWithFormat:@"%@\n%@\n%@\n%@\n%@",urlRequest.HTTPMethod,contentmd5,@"application/json",dateStr,urlRequest.URL.path];

     NSString *hmacStr = [self HMAC:secretKey :dataValue];

     NSString *authStr = [NSString stringWithFormat:@"VWS %@:%@",accessKey,hmacStr];

    [urlRequest setValue:authStr forHTTPHeaderField:@"Authorization"];

    NSURLConnection * myConnection = [[NSURLConnection allocinitWithRequest:urlRequest delegate:self startImmediately:YES];

}

-(NSString*)HMAC:(NSString*)key:(NSString*)data{

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

    const char *cData = [data 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 base64Encoding];   //// I have used the Category to find out the base64encoded String

    return hash;

}

 

-(NSString *)getUTCFormateDate:(NSDate *)localDate

{

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];

    [dateFormatter setTimeZone:timeZone];

    [dateFormatter setDateFormat:@"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'"];

    NSString *dateString = [dateFormatter stringFromDate:localDate];

    return dateString;

}

Please give some time to see the code.Am I doing wrong anything? Also I want to ask what will be the input to find out the contentMD5 string?

 

getting Error to access the VWS API

February 18, 2013 - 4:53am #2

Hi,

Could you please provide some more details as to how you are going about this etc.?

Also, have you checked the CloudReco forum for users who have had similar problems and what the diagnosis was?

cheers

N

Log in or register to post comments