Error message

User warning: The following module is missing from the file system: fast_404. For information about how to fix this, see the documentation page. in _drupal_trigger_error_with_delayed_logging() (line 1143 of /mnt/www/html/ooyala/docroot/includes/bootstrap.inc).

Código de firma de muestra (Objective C)

A continuación se incluye código de muestra Objective C para generar firmas:

#import "OoyalaAPI.h"
#import <CommonCrypto/CommonDigest.h>
#import "GTMBase64.h"
#import "GTMDefines.h"

#define ROUNDING_WINDOW 300

@implementation OoyalaAPI
/// <summary>
/// Takes in the necessary parameters to build a V2 signature for the Ooyala API
/// Use generateEncodedSignedURLWithHTTPMethod
/// </summary>
/// <param name="apiKey">
/// The API key for the Ooyala account to generate the URL for. You can find this key in the Developers tab in Backlot
/// </param>
/// <param name="secretKey">
/// The Secret key for the Ooyala account to generate the URL for. You can find this key in the Developers tab in Backlot
/// </param>
/// <param name="HTTPMethod">
/// The method to be used for the request. Possible values are: GET, POST, PUT, PATCH or DELETE
/// </param>
/// <param name="requestPath">
/// The path to use for the request
/// </param>
/// <param name="queryStringParameters">
/// A dictionary containing the list of parameters that will be included in the request.
/// </param>
/// <param name="requestBody">
/// An NSData containing the JSON representation of the data to be sent on the request. If there's no body for the request, just include an empty NSData object
/// </param>
/// <returns>
/// The signed URL to be used in the HTTP request.
/// </returns>




+ (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 not 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"];
  }
  
  //Add 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 = 0; i < [keys count]; i++) {
    NSString *key = [keys objectAtIndex:i];
    NSString *value = [parametersDictionary objectForKey:key];
    stringToSign = [stringToSign stringByAppendingFormat:@"%@=%@", key, value];
  }
  
  //Append Body
  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];
  Class _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] length:32];
  
  //Truncate signature to 43 characters
  signature = [signature substringToIndex:(NSUInteger)43]; 
  
  //Remove 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 parts of URLString
  NSString *URLString = [NSString stringWithFormat:@"%@%@",@"https://api.ooyala.com",requestPath];
  
  /**
   * Process queryStringParameters
   */
  
  //Generate mutable dictionary for parameters
  NSMutableDictionary *parametersDictionary = [NSMutableDictionary dictionaryWithDictionary:queryStringParameters];
  
  //Expires
  //Generate and add expires parameter if not 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"];
  }
  
  //Add api_key parameter
  [parametersDictionary setValue:[NSString stringWithFormat:@"%@", apiKey] forKey:@"api_key"];
  
  //Sort 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 *format = (i==0)?@"?%@=%@":@"&%@=%@";
    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];
}


@end