Tip of uploading images to tumblr using OAuth on iOS

To make my app, merge&share, I had to use OAuth to upload multiple services including tumblr.

There’s some problem, and I still don’t know which one is problem between OAuthConsumer library and tumblr.

OAuthConsumer Library makes authorization header using all parameters and if I include NSData, it may not work correctly.

– OR –

Tumblr doesn’t recognize the authorization header that OAuthConsumer made correct.

Therefore, I makes different approach to upload tumblr.

Step 1. Create OAuthHeader with only string parameters

Step 2. Create another request by using ASIFormDataRequest again and set header directly

here’s code

- (void)exportImage:(NSData *)image message:(NSString *)message withAccount:(Account *)account {

    OAToken *token = [[[OAToken alloc] initWithKey:account.token secret:account.key] autorelease];
    OAConsumer *consumer = [[[OAConsumer alloc] initWithKey:TUMBLR_API_KEY secret:TUMBLR_API_SECRET] autorelease];

    NSString *baseHost = [[account.album stringByReplacingOccurrencesOfString:@"http://" withString:@""]
                          stringByReplacingOccurrencesOfString:@"https://" withString:@""];

    NSString *requestUrl =

    [NSString stringWithFormat:@"http://api.tumblr.com/v2/blog/%@post", baseHost];

    OAMutableURLRequest *request = [[[OAMutableURLRequest alloc] initWithURL:[NSURL URLWithString:requestUrl]
                                                                    consumer:consumer
                                                                       token:token
                                                                       realm:@"http://api.tumblr.com"
                                                           signatureProvider:nil] autorelease];

    [request setHTTPMethod:@"POST"];

    [request setParameters:[NSArray arrayWithObjects:
                            [OARequestParameter requestParameter:@"type" value:@"photo"],
                            [OARequestParameter requestParameter:@"caption" value:message],
                            nil]];
    [request prepare];
    NSString *oAuthHeader = [request valueForHTTPHeaderField:@"Authorization"];
    DLog(@"OAuthHeader : %@", oAuthHeader);

    ASIFormDataRequest *req = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:requestUrl]];

    [req setRequestMethod:@"POST"];
    [req addRequestHeader:@"Authorization" value:oAuthHeader];
    [req addPostValue:@"photo" forKey:@"type"];
    [req addPostValue:message forKey:@"caption"];
    //    [req addPostValue:@"photo.jpg" forKey:@"file"];
    [req addData:image withFileName:@"photo.jpg" andContentType:@"image/jpeg" forKey:@"data"];

    [req startSynchronous];
    int statusCode = [req responseStatusCode];
    DLog(@"sending completed with status code : %d", statusCode);
    DLog(@"RESP : %@", [req responseString]);

    NSData *respData = [req responseData];

    [req release];

    if( statusCode != 201 ) {
        @throw [NSException exceptionWithName:NSLocalizedString(@"Error", @"")
                                       reason:[NSString stringWithFormat:@"status : %d", statusCode]
                                     userInfo:nil];

    }

}

Comments

comments