2011-01-13 70 views
1

我試圖使用API​​將iOS應用中的照片發送到Flickr。我已成功獲得授權,並準備好令牌。我通過HTTP POST發送照片數據,並按照格式指南在這裏。Flickr API iOS應用程序「POST size too large!」

http://www.flickr.com/services/api/upload.example.html

但我回來從API的響應...

<err code="93" msg="POST size too large! Try something smaller, mmkay?" /> 

我已閱讀,它可能是與邊界的。關於@samrowlands對這篇文章的評論,我已經翻了一番。 http://www.flickr.com/groups/api/discuss/72157605534260892/

任何幫助將是偉大的。謝謝!

UIImage *image = [UIImage imageNamed:@"photo.jpg"]; 
NSData *imageData = UIImageJPEGRepresentation(image, 0.9); 

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
[request setURL:[NSURL URLWithString:@"http://api.flickr.com/services/upload/"]]; 
[request setHTTPMethod:@"POST"]; 

NSString *boundary = [NSString stringWithString:@"---------------------------7d44e178b0434"]; 

[request addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField: @"Content-Type:"]; 

NSMutableData *body = [NSMutableData data]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"Content-Disposition: form-data; name=\"api_key\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

[body appendData:[[NSString stringWithFormat:@"%@\r\n", apiKey] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"Content-Disposition: form-data; name=\"auth_token\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

[body appendData:[[NSString stringWithFormat:@"%@\r\n", apiToken] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"Content-Disposition: form-data; name=\"api_sig\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

NSString *uploadSig = [self md5HexDigest:[NSString stringWithFormat:@"%@api_key%@auth_token%@", apiSecret, apiKey, apiToken]]; 
[body appendData:[[NSString stringWithFormat:@"%@\r\n", uploadSig] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"photo\"; filename=\"photo.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

[body appendData:imageData]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[request setHTTPBody:body]; 
+0

你能告訴我你是如何管理授權過程的嗎?我剛開始使用它,我不願意使用objectiveFlickr框架。 – neeraj 2013-02-07 06:11:40

回答

3

我通過改變Content-Type(無冒號)並在sig之後和照片之前添加邊界來獲得此項工作。感謝代碼 - 希望這有助於。

NSString *uploadSig = [[NSString stringWithFormat:@"%@api_key%@auth_token%@", secret, api_key, auth_token] MD5]; 

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
NSString *url = [NSString stringWithFormat:@"http://api.flickr.com/services/upload/"]; 
[request setURL:[NSURL URLWithString:url]]; 
[request setHTTPMethod:@"POST"]; 


NSString *boundary = [NSString stringWithString:@"---------------------------7d44e178b0434"]; 

[request addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField: @"Content-Type"]; 

NSMutableData *body = [NSMutableData data]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

[body appendData:[@"Content-Disposition: form-data; name=\"api_key\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"%@\r\n", api_key] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

[body appendData:[@"Content-Disposition: form-data; name=\"auth_token\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];  
[body appendData:[[NSString stringWithFormat:@"%@\r\n", auth_token] dataUsingEncoding:NSUTF8StringEncoding]]; 

[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"Content-Disposition: form-data; name=\"api_sig\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"%@\r\n", uploadSig] dataUsingEncoding:NSUTF8StringEncoding]]; 

[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"photo\"; filename=\"photo.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

[body appendData:imageData]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[request setHTTPBody:body]; 
+0

我使用此代碼獲取無效的身份驗證令牌消息。錯誤98.這是我的代碼,計算上傳簽名:http://pastie.org/4355056 – Homeschooldev 2012-07-29 21:17:07