2

I use Flickr API for my app to manage user's photo. I decided to make authentication using Apple's classes and it works fine for me. Now my app is authenticated and has all necessary tokens and secret keys so it's possible to perform GET requests with authentication for methods like flickr.photos.search, flickr.test.login and so on. But I spent a few days trying to perform upload using http://api.flickr.com/services/upload/ and their instructions here. They say that request should have argument 'photo' and this parameter should not be included in the signature. That is clear, but I have no idea how to implement this parameter the request.

#import "FlickrUploader.h"
#import "HMACSH1.h"
#import "Prefs.h"
#import "NSString+URLEncode.h"

@implementation FlickrUploader

- (void)uploadPhotoAtPath:(NSString*)filePath {

    NSString *upload_api_url = @"http://api.flickr.com/services/upload/";
    NSString *oauth_nonce = [NSString stringWithFormat:@"%d", 10000000 + arc4random()%1000000];
    NSString *oauth_timestamp   = [NSString stringWithFormat:@"%d", (long)[[NSDate date] timeIntervalSince1970]];
    NSString *oauth_consumer_key = CONSUMER_KEY;
    NSString *oauth_signature_method = @"HMAC-SHA1";
    NSString *oauth_version = @"1.0";
    NSString *oauth_token = [[NSUserDefaults standardUserDefaults] objectForKey:OAUTH_ACCESS_TOKEN_KEY];
//creating basestring to make signature without a 'photo' argument according to API   
    NSMutableString *basestring = [[NSMutableString alloc] initWithCapacity:8];

    [basestring appendFormat:@"&oauth_consumer_key=%@",oauth_consumer_key];
    [basestring appendFormat:@"&oauth_nonce=%@",oauth_nonce];    
    [basestring appendFormat:@"&oauth_signature_method=%@",oauth_signature_method];
    [basestring appendFormat:@"&oauth_timestamp=%@",oauth_timestamp];
    [basestring appendFormat:@"&oauth_token=%@", oauth_token];
    [basestring appendFormat:@"&oauth_version=%@",oauth_version];
//this is may class to make HMAC-SHA1 signature (it works for authentication and for GET requests)
    HMACSH1 *hMACSH1 = [[HMACSH1 alloc] init];

    NSMutableString *urlEncodedBaseString = [[NSMutableString alloc] initWithCapacity:3];
    [urlEncodedBaseString appendString:@"POST"];
    [urlEncodedBaseString appendFormat:@"&%@",[upload_api_url urlEncodedString]];
    [urlEncodedBaseString appendFormat:@"&%@",[basestring urlEncodedString]];


    NSString *oauth_token_secret = [[NSUserDefaults standardUserDefaults] objectForKey:OAUTH_TOKEN_SECRET_KEY];

    NSString *hash_key = [CONSUMER_SECRET stringByAppendingFormat:@"&%@",oauth_token_secret];

    NSString *oauth_signature = [hMACSH1 hmacSH1base64ForData:urlEncodedBaseString keyValue:hash_key];

//creating url for request    
    NSMutableString *urlString = [[NSMutableString alloc] initWithCapacity:8];

    [urlString appendFormat:@"%@",upload_api_url];
    [urlString appendFormat:@"?"];
    [urlString appendString:basestring];
    [urlString appendFormat:@"&oauth_signature=%@", oauth_signature];

    NSURL *authURL = [[NSURL alloc] initWithString:urlString]; 

    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:authURL
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:30.0];
    request.HTTPMethod = @"POST";

    UIImage *img = [UIImage imageNamed:filePath];
    NSData *imgData = UIImageJPEGRepresentation(img, 0.8);

//here I don't know what to do :((( perhaps NSOutputStream



}
Michael
  • 1,238
  • 2
  • 15
  • 26
  • offtopic: why you don't use the objective c flickr api https://github.com/lukhnos/objectiveflickr ? – CarlJ Jul 26 '12 at 07:50
  • Have you try to implement the generation of post body? – Hanon Jul 26 '12 at 08:02
  • @meccan it's a way to solve my problem, but since I managed first part why should't try the second. Any way thank you for the link. – Michael Jul 26 '12 at 09:02
  • @Hanon Yes, that is what I'm digging, but how to use 'photo' parameter, that is the question. – Michael Jul 26 '12 at 09:04
  • Can you post the related code in this post? – Hanon Jul 26 '12 at 09:06
  • @Hanon I've added some code, but actually I don't understand how to prepare body for upload. Thank you in advance for any idea. – Michael Jul 26 '12 at 09:44
  • 1
    I found a post which may help you: http://stackoverflow.com/questions/4683559/flickr-api-ios-app-post-size-too-large – Hanon Jul 26 '12 at 09:58
  • Thank you a lot! it looks like my case. How did I overlooked it? Burning with shame:((( – Michael Jul 26 '12 at 10:01

0 Answers0