我正在使用此代码通过我的应用程序将视频上传到Youtube - Uploading Videos on youtube。现在的问题是,视频永远不会被上传,我会得到这个错误:
serviceBase:<GDataServiceGoogleYouTube: 0x155e3710> objectFetcher:<GDataHTTPUploadFetcher: 0x15662c10> failedWithStatus:400 data:<errors xmlns='http://schemas.google.com/g/2005'><error><domain>GData</domain><code>InvalidRequestUriException</code><internalReason>Exception message unavailable</internalReason></error></errors>
无论我做什么。我的Youtube上传代码如下:
- (void)uploadPressed{
NSString *devKey = @"some key";
GDataServiceGoogleYouTube *service = [self youTubeService];
[service setYouTubeDeveloperKey:devKey];
NSString *kGDataServiceDefaultUser = @"isome@gmail.com";
NSString *clientID = @"googleusercontent.com";
NSURL *url = [GDataServiceGoogleYouTube youTubeUploadURLForUserID:kGDataServiceDefaultUser
clientID:clientID];
// load the file data
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"vid1.mp4"];
NSData *myData = [[NSData alloc] initWithContentsOfFile:appFile] ;
// gather all the metadata needed for the mediaGroup
NSString *titleStr = @"Survey App";
GDataMediaTitle *title = [GDataMediaTitle textConstructWithString:titleStr];
NSString *categoryStr = @"Comedy";
GDataMediaCategory *category = [GDataMediaCategory mediaCategoryWithString:categoryStr];
[category setScheme:kGDataSchemeYouTubeCategory];
NSString *descStr = @"Survey Description.";
GDataMediaDescription *desc = [GDataMediaDescription textConstructWithString:descStr];
NSString *keywordsStr = @"survey";
GDataMediaKeywords *keywords = [GDataMediaKeywords keywordsWithString:keywordsStr];
BOOL isPrivate = YES;
GDataYouTubeMediaGroup *mediaGroup = [GDataYouTubeMediaGroup mediaGroup];
[mediaGroup setMediaTitle:title];
[mediaGroup setMediaDescription:desc];
[mediaGroup addMediaCategory:category];
[mediaGroup setMediaKeywords:keywords];
[mediaGroup setIsPrivate:isPrivate];
NSString *mimeType = [GDataUtilities MIMETypeForFileAtPath:documentsDirectory
defaultMIMEType:@"video/mp4"];
// create the upload entry with the mediaGroup and the file data
GDataEntryYouTubeUpload *entry;
entry = [GDataEntryYouTubeUpload uploadEntryWithMediaGroup:mediaGroup
data:myData
MIMEType:mimeType
slug:@"vid1.mp4"];
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];
}
#pragma mark -
- (GDataServiceGoogleYouTube *)youTubeService {
static GDataServiceGoogleYouTube* service = nil;
if (!service) {
service = [[GDataServiceGoogleYouTube alloc] init];
[service setShouldCacheDatedData:YES];
[service setServiceShouldFollowNextLinks:YES];
[service setIsServiceRetryEnabled:YES];
}
// update the username/password each time the service is requested
NSString *kGDataServiceDefaultUser = @"isome@gmail.com";
NSString *password = @"passwrd";
[GDataHTTPFetcher setIsLoggingEnabled:YES];
if ([kGDataServiceDefaultUser length] > 0 && [password length] > 0) {
[service setUserCredentialsWithUsername:kGDataServiceDefaultUser
password:password];
} else {
// fetch unauthenticated
[service setUserCredentialsWithUsername:nil
password:nil];
}
NSString *devKey = @"some key";
[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]