Youtube API Authentication - Iphone

2019-04-26 07:09发布

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?

1条回答
不美不萌又怎样
2楼-- · 2019-04-26 07:25

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 'x...@gmail.com' 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_id@gmail.com 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..!!

查看更多
登录 后发表回答