How do I upload an NSImage(NSData)to Twitpic with OAMutableURLRequest?
- by timothy5216
I'm using OAConsumer in my xAuth twitterEngine and i'm adding Twitpic OAuth Echo to it. But it won't POST the NSData. here is some of my code:
//other file
NSArray *reps = [[imageToUpload image] representations];
NSData *imageData = [NSBitmapImageRep representationOfImageRepsInArray:reps
usingType:NSJPEGFileType
properties:nil];
[twitter testUploadImageData:imageData withMessage:@"Hello WORLD!!" toURL:[NSURL URLWithString:uploadURL.stringValue]];
//
- (void)testUploadImageData:(NSData *)data withMessage:(NSString *)message toURL:(NSURL *)url;
{
//url = @"http://api.twitpic.com/2/upload.xml"
//message = @"Hello WORLD!!"
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *String = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"dataString: %@",String);
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url
consumer:self.consumer
token:_accessToken
realm:nil
signatureProvider:nil];
// Setup POST body
[request setHTTPMethod:@"POST"];
//NSString *stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
//NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", stringBoundary];
// NSString *stringBoundarySeparator = [NSString stringWithFormat:@"\r\n--%@\r\n", stringBoundary];
/*
NSMutableString *postString = [NSMutableString string];
[postString appendString:@"\r\n"];
[postString appendString:stringBoundarySeparator];
[postString appendString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"message\"\r\n\r\n%@", message]];
[postString appendString:stringBoundarySeparator];
[postString appendString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"media\"; filename=\"%@\"\r\n", @"file.jpg"]];
[postString appendString:@"Content-Type: image/jpg\r\n"];
[postString appendString:@"Content-Transfer-Encoding: binary\r\n\r\n"];
// Setting up the POST request's multipart/form-data body
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[postString dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:data];
[request setHTTPBody:postBody];
*/
[request setHTTPMethod:@"POST"];
NSString *thing = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"%@",thing);
[request setParameters:[NSArray arrayWithObjects:
[OARequestParameter requestParameterWithName:@"oauth_token" value:_accessToken.key],
[OARequestParameter requestParameterWithName:@"X-Auth-Service-Provider" value:@"https://api.twitter.com/1/account/verify_credentials.json"],
[OARequestParameter requestParameterWithName:@"key" value:@"my-key-here :P"],
[OARequestParameter requestParameterWithName:@"message" value:message],
//iv'e changed this many times. I was just trying this to see if it works
[OARequestParameter requestParameterWithName:@"media" value:thing],
nil]];
OAAsynchronousDataFetcher *dataFetcher = [[OAAsynchronousDataFetcher alloc] init];
[dataFetcher initWithRequest:request delegate:self
didFinishSelector:@selector(uploadDidUpload:withData:)
didFailSelector:@selector(uploadDidFail:withData:)];
[dataFetcher start];
[dataFetcher release];
[request release];
[pool drain];
}
I'm authenticated but it still won't POST the data :(