Objective-C Google网络相册API - 尝试上传图片时出现奇怪错误

时间:2011-08-19 02:57:27

标签: iphone objective-c gdata picasa

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

我几乎完全复制了示例项目中的代码,但是 每次尝试上传图片时都会收到此错误消息: “failedWithStatus:400数据:照片数据不能为空。”。

这是我的代码:

- (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 数据:照片数据不能为空。“可能意味着。请帮忙!

1 个答案:

答案 0 :(得分:2)

而不是使用以下方法:

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

//要获取相册网址,请使用以下网址:

NSURL *albumURL =
[NSURL URLWithString:kGDataGooglePhotosDropBoxUploadURL]

//其中

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

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

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