Profile Image Update Twitter API
Posted
by jshollax
on Stack Overflow
See other posts from Stack Overflow
or by jshollax
Published on 2010-01-17T21:01:18Z
Indexed on
2010/04/18
23:03 UTC
Read the original article
Hit count: 315
I am trying to upload a profile image to twitter using their API. I found some good examples on the web on how to format the URLRequest, however I just can't seem to get this to work. Any help would be appreciated. The URL that I am using from Twitter API is http://twitter.com/account/update_profile_image.json
-(void)uploadImageForAccount:(NSURL *)theURL intUserName:(NSString *)_userName initPassWord:(NSString *)_passWord image:(NSData *)image{ userName = [_userName retain]; passWord = [_passWord retain];
UIApplication *app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES;
requestedData = [[NSMutableData alloc] initWithLength:0];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60];
[theRequest setHTTPMethod:@"POST"];
NSString *stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", stringBoundary];
[theRequest addValue:contentType forHTTPHeaderField:@"Content-Type"];
// Credentials
NSString *creds = [NSString stringWithFormat:@"%@:%@", userName, passWord];
[theRequest addValue:[NSString stringWithFormat:@"Basic %@",[self base64encode:creds]] forHTTPHeaderField:@"Authorization"];
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"\r\n\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"source\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"canary"] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *mimeType = mimeType = @"image/jpeg";
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"media\"; filename=\"%@\"\r\n", @"TNImage.jpeg"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n", mimeType] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Transfer-Encoding: binary\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:image];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[theRequest setHTTPBody:postBody];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest
delegate:self
startImmediately:YES];
if(DEBUG) NSLog(@"Connection = %@",[connection description]);
if (connection == nil) {
if(DEBUG) NSLog(@"Connection was Nil");
}
}
© Stack Overflow or respective owner