Search code examples
videoyoutube-apigdata-apiuploading

uploading video from app to youTube iphone sdk


I am tring to uplosd a video taken in my app to YouTube. I have linked the title, the usename , the password, the path to the movie, its length, keywords, and the catagories along with my app key. Yet when I try to upload I get the following message.

2012-12-02 21:29:33.235 my little world app[12790:907] * Assertion failure in -GDataServiceBase fetchObjectWithURL:objectClass:objectToPost:ETag:httpMethod:delegate:didFinishSelector:completionHandler:retryInvocationValue:ticket:, /Users/nilesh/gdata-objectivec-client-read-only/Source/BaseClasses/GDataServiceBase.m:549 2012-12-02 21:29:33.238 my little world app[12790:907] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'GTMHTTPUploadFetcher needed'

This is the code I use to try to upload it.

- (IBAction)publish: (id) sender;{

    self.mProgressView.hidden =NO;

    NSString *audioName = [pictureDictionary4 objectForKey:@"photoVideokey"];
    NSArray *pathsa = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectorya = [pathsa objectAtIndex:0];
    //Get a full path to the image in the documents directory.
    NSString *fullPatha = [documentsDirectorya stringByAppendingPathComponent:audioName];
    NSError *error = nil;
    NSDictionary *attributes = [[NSFileManager defaultManager]
                                attributesOfItemAtPath:fullPatha error:&error];

    if (!error) {
        NSNumber *size = [attributes objectForKey:NSFileSize];
    }

    NSString *devKey = @"xxxxxx";

    GDataServiceGoogleYouTube *service = [self youTubeService];
    [service setYouTubeDeveloperKey:devKey];

    NSString *username = self.accountView.text;
  NSURL *url = [GDataServiceGoogleYouTube youTubeUploadURLForUserID:username];
    NSData *data = [NSData dataWithContentsOfFile:fullPatha];
    NSString *filename = [pictureDictionary4 objectForKey:@"photoVideokey"];

    // gather all the metadata needed for the mediaGroup
    NSString *titleStr =self.movieNameField.text;
    GDataMediaTitle *title = [GDataMediaTitle textConstructWithString:titleStr];

    NSString *categoryStr = self.catagoryField.text;
    GDataMediaCategory *category = [GDataMediaCategory mediaCategoryWithString:categoryStr];
    [category setScheme:kGDataSchemeYouTubeCategory];

    NSString *descStr = self.descpView.text;
    GDataMediaDescription *desc = [GDataMediaDescription textConstructWithString:descStr];

    NSString *keywordsStr = self.tagsField.text;
    GDataMediaKeywords *keywords = [GDataMediaKeywords keywordsWithString:keywordsStr];

    BOOL isPrivate = NO;

    GDataYouTubeMediaGroup *mediaGroup = [GDataYouTubeMediaGroup mediaGroup];
    [mediaGroup setMediaTitle:title];
    [mediaGroup setMediaDescription:desc];
    [mediaGroup addMediaCategory:category];
    [mediaGroup setMediaKeywords:keywords];
    [mediaGroup setIsPrivate:isPrivate];

    NSString *mimeType = [GDataUtilities MIMETypeForFileAtPath:fullPatha
                                               defaultMIMEType:@"video/mp4"];

    // create the upload entry with the mediaGroup and the file data
    GDataEntryYouTubeUpload *entry;
    entry = [GDataEntryYouTubeUpload uploadEntryWithMediaGroup:mediaGroup
                                                          data:data
                                                      MIMEType:mimeType
                                                          slug:filename];

    SEL progressSel = @selector(ticket:hasDeliveredByteCount:ofTotalByteCount:);
    [service setServiceUploadProgressSelector:progressSel];

    GDataServiceTicket *ticket;
    ticket = [service fetchEntryByInsertingEntry:entry
                                      forFeedURL:url
                                        delegate:self
                               didFinishSelector:@selector(uploadTicket:finishedWithEntry:error:)];

    [self setUploadTicket:ticket];

    NSLog(@"%@",length.text);
    NSLog(@"%@",number.text);
     NSLog(@"%@",accountView.text);
     NSLog(@"%@",tagsField.text);
     NSLog(@"%@",catagoryField.text);
     NSLog(@"%@",movieNameField.text); 
     NSLog(@"%@",descpView.text);
     NSLog(@"%@",PasswordDisplayField.text);
     NSLog(@"%@",ViewingField.text);

    NSLog(@"%@",username);
    NSLog(@"%@",url);
    NSLog(@"%@",filename);
    NSLog(@"%@",titleStr);
    NSLog(@"%@",category);
    NSLog(@"%@",categoryStr);
    NSLog(@"%@",descStr);
    NSLog(@"%@",desc);
    NSLog(@"%@",keywords);
     NSLog(@"%@",keywords);
     NSLog(@"%@",keywordsStr);



}





    - (GDataServiceGoogleYouTube *)youTubeService {

        static GDataServiceGoogleYouTube* service = nil;

        if (!service) {
            service = [[GDataServiceGoogleYouTube alloc] init];


            [service setServiceShouldFollowNextLinks:YES];
            [service setIsServiceRetryEnabled:YES];
        }

        // update the username/password each time the service is requested
        NSString *username = [accountView text];
        NSString *password = [PasswordDisplayField text];

        if ([username length] > 0 && [password length] > 0) {
            [service setUserCredentialsWithUsername:username
                                           password:password];
        } else {
            // fetch unauthenticated
            [service setUserCredentialsWithUsername:nil
                                           password:nil];
        }

        NSString *devKey =  @"AI39si5rnzA1o82wzQj_kFFJBQRNEPv29OKD9hx6RxLPzvacvfvLFi_uI9eNj0uq8Cul8VQnBbDs4CAvq7ViKq-RnVCgh3OVBQ";
        [service setYouTubeDeveloperKey:devKey];

        return service;
    }

    // progress callback
    - (void)ticket:(GDataServiceTicket *)ticket
hasDeliveredByteCount:(unsigned long long)numberOfBytesRead
ofTotalByteCount:(unsigned long long)dataLength {

    [mProgressView setProgress:(double)numberOfBytesRead / (double)dataLength];
}

    // upload callback
    - (void)uploadTicket:(GDataServiceTicket *)ticket
finishedWithEntry:(GDataEntryYouTubeVideo *)videoEntry
error:(NSError *)error {
    if (error == nil) {
        // tell the user that the add worked
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Uploaded!"
                                                        message:[NSString stringWithFormat:@"%@ succesfully uploaded",
                                                                 [[videoEntry title] stringValue]]
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];

        [alert show];

    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!"
                                                        message:[NSString stringWithFormat:@"Error: %@",
                                                                 [error description]]
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];

        [alert show];

    }
    [mProgressView setProgress: 0.0];

    [self setUploadTicket:nil];
}

#pragma mark -
#pragma mark Setters

    - (GDataServiceTicket *)uploadTicket {
        return mUploadTicket;
    }

    - (void)setUploadTicket:(GDataServiceTicket *)ticket {

        mUploadTicket = ticket;
    }

I would really appreciate some direction on this. Thanks


Solution

  • I finally got this part working by adding all the correct .h files to my app and importing them, including the right GTMHTTPUploadFetcher file. Thanks for the help.