1

当我尝试在 YouTube 上上传视频时,我的应用程序出现以下错误。

**2014-02-21 15:47:19.355 LandroVA[1088:60b] serviceBase: objectFetcher:GTMHTTPUploadFetcher 0x156436f0 ( https://uploads.gdata.youtube.com/resumable/feeds/api/users/default/uploads )

failedWithStatus:401 数据:令牌无效 2014-02-21 15:47:31.886 LandroVA [1088:60b] 错误域 = com.google.HTTPStatus 代码 = 401 “操作无法完成。(令牌无效)” UserInfo = 0x1686e7b0 {错误=令牌无效,NSLocalizedFailureReason=(令牌无效)}**

这是我的代码

- (void)uploadVideoFile :(GTMOAuth2Authentication*)auth{
[self getNewAccessToken];
[mTitleField setText: [shareVidTitleArr objectAtIndex:0]];
[mDescriptionField setText: [shareVidDescpArr objectAtIndex:0]];
[mKeywordsField setText: @"Hi"];
[mCategoryField setText: @"Entertainment"];

NSString *devKey = [mDeveloperKeyField text];

GDataServiceGoogleYouTube *service = [self youTubeService];
//auth.accessToken = (NSString*)[[NSUserDefaults standardUserDefaults] objectForKey:@"access_token"];
auth.refreshToken = (NSString*)[[NSUserDefaults standardUserDefaults] objectForKey:@"refresh_token"];
auth.tokenType = (NSString*)[[NSUserDefaults standardUserDefaults] objectForKey:@"token_type"];

[service setAuthorizer:auth];

//[service setAuthToken:[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"access_token"]]];

[service setAuthToken:[NSString stringWithFormat:@"%@",auth.accessToken]];
[service setYouTubeDeveloperKey:devKey];

//NSURL *url = [GDataServiceGoogleYouTube youTubeUploadURLForUserID:[auth.parameters objectForKey:@"email"]];

NSURL *url = [GDataServiceGoogleYouTube youTubeUploadURLForUserID:kGDataServiceDefaultUser];

ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
[assetLibrary assetForURL:[NSURL URLWithString:self.videoUrl] resultBlock:^(ALAsset *asset)
 {
     ALAssetRepresentation *rep = [asset defaultRepresentation];
     Byte *buffer = (Byte*)malloc(rep.size);
     NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
     NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];//this is NSData may be what you


     //NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.videoUrl]];

     // NSString *filename = [path lastPathComponent];
     NSLog(@"Upload Data %@",self.videoUrl);


     // 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];
     //[mediaGroup setProperty:filename forKey:@"vidID"];

     NSString *mimeType = [GDataUtilities MIMETypeForFileAtPath:self.videoUrl defaultMIMEType:@"video/quicktime"];

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

     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];


  }
   failureBlock:^(NSError *err) {
   NSLog(@"Error: %@",[err localizedDescription]);
       [self setFailureStatus];
  }
 ];

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

// load the file data
// NSArray *searchPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
// NSString *docDir = [searchPath objectAtIndex:0];
// NSString  *path = [docDir stringByAppendingPathComponent:[shareVidArr objectAtIndex:0]];      
}

注意:: 我正在使用带有 Oauth2 的 GDATA API。

请指导我在哪里做错了?

4

0 回答 0