1

我正在使用 API 的 Objective-C 版本。

我几乎完全复制了示例项目中的代码,但每次尝试上传图片时都会出现此错误:“failedWithStatus:400 data:Photo data must not be empty.”。

这是我的代码:

- (void)uploadToPicasa
{

   // make a new entry for the photo
   GDataEntryPhoto *newEntry = [GDataEntryPhoto photoEntry];

   // set a title, description, and timestamp
   [newEntry setTitleWithString:@"Title!"];
   [newEntry setPhotoDescriptionWithString:@"Description!"];
   [newEntry setTimestamp:[GDataPhotoTimestamp timestampWithDate:
[NSDate date]]];

   // attach the NSData and set the MIME type for the photo
   UIImage *earth_image = [UIImage imageNamed:@"earth.jpg"];
   NSData *earth_data = UIImageJPEGRepresentation(earth_image, 1.0);

   [newEntry setPhotoData:earth_data];

   NSString *mimeType = @"image/jpeg";

   [newEntry setPhotoMIMEType:mimeType];

   // the slug is just the upload file's filename
   [newEntry setUploadSlug:@"earth.jpg"];

   // make service tickets call back into our upload progress
selector
   GDataServiceGooglePhotos *service = [self googlePhotosService];

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

   // Get URL for Picasa
   NSURL *uploadURL = [GDataServiceGooglePhotos
photoFeedURLForUserID:@"CORRECT_USERNAME"

albumID:nil

albumName:nil

photoID:nil

kind:nil

access:nil];

   // insert the entry into the album feed
   GDataServiceTicket *ticket;
   ticket = [service fetchEntryByInsertingEntry:newEntry
                                     forFeedURL:uploadURL
                                       delegate:self

didFinishSelector:@selector(addPhotoTicket:finishedWithEntry:error:)];

   // no need for future tickets to monitor progress
   [service setServiceUploadProgressSelector:nil];
}

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

   NSLog([NSString stringWithFormat:@"%d", numberOfBytesRead /
dataLength]);
}

// photo add callback
- (void)addPhotoTicket:(GDataServiceTicket *)ticket
    finishedWithEntry:(GDataEntryPhoto *)photoEntry
                error:(NSError *)error {

   if (error == nil) {
       NSLog(@"SHOULD BE UPLOADED MAYBE");
   } else {
       NSLog(@"THERE WAS AN ERROR");
   }
}

- (GDataServiceGooglePhotos *)googlePhotosService {

   static GDataServiceGooglePhotos* service = nil;

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

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

   // update the username/password each time the service is requested
   NSString *username = @"CORRECT_USERNAME";
   NSString *password = @"CORRECT_PASSWORD";
   if ([username length] && [password length]) {
       [service setUserCredentialsWithUsername:username
                                      password:password];
   } else {
       [service setUserCredentialsWithUsername:nil
                                      password:nil];
   }

   return service;
}

我使用断点来确认 NSData 不是 nil,并且是有问题的图像,所以我不确定“failedWithStatus:400 data:Photo data must not be empty”是什么。可能意味着。请帮忙!

4

1 回答 1

2

而不是使用以下方法:

 NSURL *albumURL = [GDataServiceGooglePhotos
                       photoFeedURLForUserID:username albumID:kGDataGooglePhotosDropBoxAlbumID
                       albumName:nil photoID:nil kind:nil access:nil];

// 获取专辑 url 使用这个:

NSURL *albumURL =
[NSURL URLWithString:kGDataGooglePhotosDropBoxUploadURL]

// 在哪里

kGDataGooglePhotosDropBoxUploadURL = "https://photos.googleapis.com/data/upload/resumable/media/create-session/feed/api/user/default/albumid/default";

我在我的应用程序中尝试过,它的工作完美......

您也可以在 gdata api 中找到此 url。

于 2011-10-01T13:17:23.073 回答