【问题标题】:Objective-C Google Web Albums API - Strange error when trying to upload imageObjective-C Google Web Albums API - 尝试上传图片时出现奇怪的错误
【发布时间】:2011-10-30 07:08:12
【问题描述】:

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

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

这是我的代码:

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

【问题讨论】:

  • 有人可以给我发这个代码吗?我什至无法开始,因为我得到了 Mac 支持而不是 iOS 的代码。另外,我不了解 Google API 的集成。

标签: iphone objective-c gdata picasa


【解决方案1】:

不要使用以下方法:

 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 中找到此 url。

【讨论】:

  • 你能把代码发给我吗?我什至无法理解从哪里开始。尝试了很多样本​​,但有些样本丢失了一个文件,而另一些则缺少另一个文件。急需这个,请帮忙。
  • 这个代码太旧了,现在这个api被弃用了......你需要在与api集成后使用新的api来上传照片你可以使用相同的url来上传照片。..
  • 我可以通过 Google plus api 进行身份验证登录并获取用户名,但我无法获取用于上传的客户端密钥和密钥。
  • 我完成了 Oauth,获取 Oauth 对象。现在我必须上传图片,但我不希望用户输入他们的用户名和密码“[service setUserCredentialsWithUsername:uname password:pwd]”采取他们已经通过 Oauth 的方式。有什么替代方法可以做到这一点?
猜你喜欢
  • 1970-01-01
  • 2020-10-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 2020-12-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多