2011-08-19 14 views
1

我正在使用Objective-C版本的API。Objective-C Google網絡相冊API - 嘗試上傳圖片時出現奇怪的錯誤

我已經從示例項目中幾乎完全複製了代碼,但每次嘗試上傳圖片時都會收到此錯誤: 「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的不爲零,而且是有問題的 形象,所以我不知道什麼是「failedWithStatus:400 數據:照片數據不能爲空。「可能意味着。請幫忙!

+0

有人可以給我發送代碼。我無法啓動,因爲我得到的不是iOS的Mac支持代碼。另外,我不瞭解Google API的集成。 – Swati

回答

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中找到這個網址。

+0

CAn您發送我的代碼。我無法理解甚至從何處開始。嘗試了很多樣品,但有些只有一個文件丟失,另一個有另一個。急需這個請幫忙。 – Swati

+0

這是太舊的代碼,現在這個api被刪除了......你需要使用新的API後與api集成上傳照片,你可以使用相同的URL上傳照片可能.. – GameLoading

+0

我能夠通過Google plus進行身份驗證登錄api並獲取用戶名,但我無法獲得客戶端上傳密鑰和密鑰。 – Swati

相關問題