If you don't find an answer, please click here to post your question.
Reply
Highlighted
Occasional Visitor
Posts: 3
Registered: ‎05-16-2018

Re: Video Upload Signing - Objective C using Ooyala API

hello 

this is an example how it is been done 

 hope this is gonnaa  be usefull for you !

This following is objective C sample code for generating signatures:

#import "OoyalaAPI.h"
#import
#import "GTMBase64.h"
#import "GTMDefines.h"

#outline ROUNDING_WINDOW three hundred

@implementation OoyalaAPI
///
/// Takes in the essential parameters to build a V2 signature for the Ooyala API
/// Use generateEncodedSignedURLWithHTTPMethod
///
///
/// The API key for the Ooyala account to generate the URL for. you can locate this key inside the builders tab in Backlot
///
///
/// the name of the game key for the Ooyala account to generate the URL for. you may locate this key in the developers tab in Backlot
///
///
/// The approach to be used for the request. feasible values are: GET, submit, placed, PATCH or DELETE
///
///
/// The course to use for the request
///
///
/// A dictionary containing the list of parameters with the intention to be protected in the request.
///
///
/// An NSData containing the JSON representation of the records to be sent on the request. If there may be no body for the request, simply encompass an empty NSData item
///
///
/// The signed URL to be used in the HTTP request.
///

 


+ (NSString *)URLEncodeString:(NSString *)string
{
CFTypeRef URLEncodedCFType = CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)string, NULL, (CFStringRef)@"!*'"();:@&=+$,/?%#[]% ", kCFStringEncodingUTF8);
NSString *URLEncodedString = [NSString stringWithFormat:@"%@",URLEncodedCFType];
CFRelease(URLEncodedCFType);
return URLEncodedString;
}

- (NSString *)generateEncodedSignatureWithHTTPMethod:(NSString *)HTTPMethod requestPath:(NSString *)requestPath queryStringParameters:(NSDictionary *)queryStringParameters secretKey:(NSString *)secretKey apiKey:(NSString *)apiKey andRequestBody:(NSData *)requestBody
{

//
// Generate stringToSign
//

//Concatenate first parameters to stringToSign
NSString *stringToSign = [NSString stringWithFormat:@"%@%@%@",secretKey,HTTPMethod,requestPath];

//Generate mutable dictionary for parameters
NSMutableDictionary *parametersDictionary = [NSMutableDictionary dictionaryWithDictionary:queryStringParameters];

//Expires
//Generate and add expires parameter if now not already present
//Default expires time: 5min = 300s
if(![parametersDictionary objectForKey:@"expires"]){
NSNumber *expiresWindow = [NSNumber numberWithInt:15];
NSUInteger timestamp = (lengthy)[[NSDate date] timeIntervalSince1970] + [expiresWindow intValue];
timestamp += [[NSNumber numberWithInt:ROUNDING_WINDOW] intValue] - (timestamp % [[NSNumber numberWithInt:ROUNDING_WINDOW] intValue] );
[parametersDictionary setValue:[NSString stringWithFormat:@"%d", timestamp] forKey:@"expires"];
}

//upload api_key parameter
[parametersDictionary setValue:[NSString stringWithFormat:@"%@", apiKey] forKey:@"api_key"];

//sort parameters and append to stringToSign
NSArray *keys = [[parametersDictionary allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
for (NSUInteger i = zero; i < [keys count]; i++) {
NSString *key = [keys objectAtIndex:i];
NSString *value = [parametersDictionary objectForKey:key];
stringToSign = [stringToSign stringByAppendingFormat:@"%@=%@", key, value];
}

//Append frame
NSString *requestBodyString = [[NSString alloc] initWithData:requestBody encoding:NSUTF8StringEncoding];
stringToSign = [stringToSign stringByAppendingFormat:@"%@",requestBodyString];
[requestBodyString release];

//
// Generate signature from stringToSign
//

unsigned char hashedChars[32];
NSUInteger i;

//Generate SHA-256 in Base64
CC_SHA256([stringToSign UTF8String], [stringToSign lengthOfBytesUsingEncoding:NSUTF8StringEncoding], hashedChars);
NSData *hashedData = [NSData dataWithBytes:hashedChars length:32];
elegance _gtmBase64 = NSClassFromString(@"GTMBase64");
if (!_gtmBase64){
[NSException raise:@"OOMissingLibraryException" format:@"GTMBase64 is not pressent on the current Target. Add GTMBase64.h, GTMBase64.m and GTMDefines.h from the Google Toolbox for Mac (http://code.google.com/p/google-toolbox-for-mac/)"];
}
NSString *signature = [_gtmBase64 stringByEncodingBytes:[hashedData bytes] period:32];

//Truncate signature to forty three characters
signature = [signature substringToIndex:(NSUInteger)43];

//put off from signature trailing = signs
for (i = [signature length] - 1; [signature characterAtIndex:i] == '='; i = [signature length] - 1) {
signature = [signature substringToIndex:i];
}

//URL-encode signature
return [OoyalaAPI URLEncodeString:signature];
}

 

- (NSURL *)generateEncodedSignedURLWithHTTPMethod:(NSString *)HTTPMethod requestPath:(NSString *)requestPath queryStringParameters:(NSDictionary *)queryStringParameters apiKey:(NSString *)apiKey secretKey:(NSString *)secretKey andRequestBody:(NSData *)requestBody
{
//Append first components of URLString
NSString *URLString = [NSString stringWithFormat:@"%@%@",@"https://api.ooyala.com",requestPath];

/**
* procedure queryStringParameters
*/

//Generate mutable dictionary for parameters
NSMutableDictionary *parametersDictionary = [NSMutableDictionary dictionaryWithDictionary:queryStringParameters];

//Expires
//Generate and add expires parameter if no longer already present
//Default expires time: 5min = 300s
if(![parametersDictionary objectForKey:@"expires"]){
NSNumber *expiresWindow = [NSNumber numberWithInt:15];
NSUInteger timestamp = (long)[[NSDate date] timeIntervalSince1970] + [expiresWindow intValue];
timestamp += [[NSNumber numberWithInt:ROUNDING_WINDOW] intValue] - (timestamp % [[NSNumber numberWithInt:ROUNDING_WINDOW] intValue] );
[parametersDictionary setValue:[NSString stringWithFormat:@"%d", timestamp] forKey:@"expires"];
}

//upload api_key parameter
[parametersDictionary setValue:[NSString stringWithFormat:@"%@", apiKey] forKey:@"api_key"];

//kind parameters and append to URLString
NSArray *keys = [[parametersDictionary allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
for (NSUInteger i = 0; i < [keys count]; i++) {
NSString *key = [keys objectAtIndex:i];
NSString *value = [parametersDictionary objectForKey:key];
NSString *layout = (i==zero)?@"?%@=%@":@"&%@=%@";
URLString = [URLString stringByAppendingFormat:format, [OoyalaAPI URLEncodeString:key], [OoyalaAPI URLEncodeString:value]];
}

//Append the signature
URLString = [URLString stringByAppendingFormat:@"&signature=%@",[self generateEncodedSignatureWithHTTPMethod:HTTPMethod requestPath:requestPath queryStringParameters:queryStringParameters secretKey:secretKey apiKey:apiKey andRequestBody:requestBody]];

return [NSURL URLWithString:URLString];
}

regards

     jayesh