Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Youtube API Authentication - Iphone

I am trying to upload video using sample code of youtube api. When i press upload button, the progress bar finishes its process, but once when it reaches end of point i get error. Error description is as follows :

YouTubeTest[2149:f803] error - Error Domain=com.google.GDataServiceDomain Code=400 "The operation couldn’t be completed. (com.google.GDataServiceDomain error 400.)" UserInfo=0x69d5bd0 {}

This is code for upload button pressed

- (IBAction)uploadPressed:(id)sender {
    [self.view resignFirstResponder];
    NSString *devKey = [mDeveloperKeyField text];

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

    NSString *username = [mUsernameField text];
    NSString *clientID = [mClientIDField text];

    NSURL *url = [GDataServiceGoogleYouTube youTubeUploadURLForUserID:username
                                                             clientID:clientID];

    // load the file data
    NSString *path = [[NSBundle mainBundle] pathForResource:@"YouTubeTest" ofType:@"m4v"]; 
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSString *filename = [path lastPathComponent];

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

    NSString *categoryStr = [mCategoryField text];
    GDataMediaCategory *category = [GDataMediaCategory mediaCategoryWithString:categoryStr];
    [category setScheme:kGDataSchemeYouTubeCategory];

    NSString *descStr = [mDescriptionField text];
    GDataMediaDescription *desc = [GDataMediaDescription textConstructWithString:descStr];

    NSString *keywordsStr = [mKeywordsField text];
    GDataMediaKeywords *keywords = [GDataMediaKeywords keywordsWithString:keywordsStr];

    BOOL isPrivate = mIsPrivate;

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

    NSString *mimeType = [GDataUtilities MIMETypeForFileAtPath:path
                                               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];

}

I have set up developer key and client key correctly from API Dashboard.

I am running this on simulator. Is it we can not upload videos from simulator?

Please guide me where am i going wrong?

like image 901
innodeasapps Avatar asked Jun 11 '12 20:06

innodeasapps


People also ask

How do I authenticate YouTube API?

First, set up your project, website, or app and then get a YouTube API key. This key will let you access YouTube's Live Streaming API. Don't forget to register your app with Google so that your API requests are successfully authenticated.

Can I use YouTube API for free?

Yes, using the YouTube API does not incur any monetary cost for the entity calling the API. If you go over your quota an 403 Error will be returned by the API.

How do I enable YouTube API v3?

In Google API dashboard, click Library on the left menu. Click YouTube Data API v3, then click the Enable button to enable the API service.


1 Answers

Error Solved... When using a Google account to upload video to YouTube, some functions of the GData Objective-C require the Gmail account as parameter and some require the YouTube linked account as parameter.

When you call '- (void)setUserCredentialsWithUsername:(NSString *) username password:(NSString *)password;' in GDataServiceBase, the username should be the Gmail account, for example '[email protected]' and the password should be the password of the Gmail account.

But when you call '+ (NSURL *)youTubeUploadURLForUserID:(NSString *) userID clientID:(NSString *)clientID;' in GDataServiceGoogleYouTube, the userID parameter should be the YouTube linked account and the password the password of the Gmail account.

I was using [email protected] to log in where now i am just using email_id to log in. What a silly error!.. But took me 2 full days to solve.. Duhhh..!!

like image 74
innodeasapps Avatar answered Sep 28 '22 16:09

innodeasapps