Thursday, 20 February 2014

iPhone Development:Integrate Gyft API in IOS






Gyft provides a series of REST APIs, which enables reseller’s third party applications to offer their own end-to-end virtual gift card programs with minimal effort. In one of my application, i came across this API. The most pain for the IOS developer in their documentation is taht they did not provide objective C source to help iPhone developer integrating their API easily.
According to their documentation  one need to follow below steps to query their server.


I will take you step by step, how to integrate and sign the request in Objective C. First we need to get difference in seconds, between current date and midnight January 1, 1970 UTC as mentioned above.
To get this difference we will use timeIntervalSince1970 method provided in Objective C.
 time_t unixTime = (time_t) [[NSDate date] timeIntervalSince1970];
Now in order to create our final URL  we need to create a signature which is a combination of  our API_KEY, API_SECRET, time difference and these things are encoded with SHA256 and then is hex encoded. First we created our signature string
    NSMutableString *sigStr = [NSMutableString stringWithString:@"45d584zuk922je7qqbtkxp38"];
    sigStr=(NSMutableString*)[sigStr stringByAppendingString:@"jP525wJtGK2xW"];
    sigStr=(NSMutableString*)[sigStr stringByAppendingFormat:@"%ld",unixTime];


Now second step according to documentation is to encode signature string (here referred as sigStr) with SHA256.
Here is the SHA256 encoding method for objective C
-(NSString*)sha256HashFor:(NSString*)input
{
    const char* str = [input UTF8String];
    unsigned char result[CC_SHA256_DIGEST_LENGTH];
    CC_SHA256(str, strlen(str), result);
   
    NSMutableString *ret = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH*2];
    for(int i = 0; i<CC_SHA256_DIGEST_LENGTH; i++)
    {
        [ret appendFormat:@"%02x",result[i]];
    }
    return ret;
}


NOTE:- Here we did not need to hex encode the string as its already hex encode in SHA256.(This thing takes my 3 days as i always getting response not authorized from gyft server)
Now as we had our method that will give us our encoded string we are ready to make our request to gyft server.

 NSData *data =[[self sha256:str] dataUsingEncoding:NSUTF8StringEncoding];
 NSString *urlString = [NSString stringWithFormat:@"http://sandbox.gyft.com/v1/reseller/account?api_key=45d584zuk922je7qqbtkxp38&sig=%@",[self sha256:sigStr]]; urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];

 [req setHTTPMethod:@"GET"];
 [req setValue:[NSString stringWithFormat:@"%ld",unixTime] forHTTPHeaderField:@"x-sig-timestamp"];

 NSData *datareturned = [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:&error];
 id mainDict = [NSJSONSerialization JSONObjectWithData:datareturned options:NSJSONReadingAllowFragments error:&error];
 NSLog(@"dict == %@",mainDict);


No comments:
Write comments