Skip to content

Image post to Twitter using yfrog

Compared to Facebook, uploading images to Twitter via yfrog is relatively easy…

YFROGuploadAndPost – imageshackapi

0 thoughts on “Image post to Twitter using yfrog”

  1. Alan Taylor

    Is there any way you could go into more detail with this? I have code for posting to Twitter but I just can’t get around using YFrog to upload an image and Twitter message 🙁

    Any help would be much appreciated! Here is the code that I used to post to Twitter and I was able to use it to make small changes… http://blogs.oreilly.com/iphone/2008/08/tweeting.html

  2. From that O’Reilly post, you see how to do a POST. So to use YFrog to post an image…

    You need to send a POST to http://yfrog.com/api/uploadAndPost with these params:

    username – Twitter username
    password – Twitter password
    url – url to the image online to post (I use this for my NoonView app which uploads images to my server so I have a url) or you can use media (below)
    message – whatever Tweet message you want
    tags – I use lat/lng like “geo:lat=%f,geo:lon=%f”

    media – Binary image or video data; either media or url parameter is required (see http://code.google.com/p/imageshackapi/wiki/YFROGuploadAndPost)

    I usually use a multi-part POST body which is necessary for uploading an actual file. You can see a good example of multi-part POST for file upload here: http://stackoverflow.com/questions/125306/how-can-i-upload-a-photo-to-a-server-with-the-iphone

    Here’s some more good links:
    http://www.wetware.co.nz/blog/2009/03/upload-a-photo-from-iphone-64-encoding-multi-part-form-data/#more-69
    http://www.cocoadev.com/index.pl?HTTPFileUpload
    http://lingonikorgsource.wordpress.com/2008/11/28/olbtwitpicengine-twitter-image-uploading-code/

    I hope that helps!

  3. Alan Taylor

    Thanks for your help – but even though my code all runs through fine nothing is actually getting posted to Twitter.

    This is the code I have – but with the amount of time I’ve spent on this now I think I should just cut my losses and only post text :/

    imageData = UIImagePNGRepresentation(image1);

    // Setup POST body
    stringBoundary = [NSString stringWithString:@”0xKhTmLbOuNdArY”];
    contentType = [NSString stringWithFormat:@”multipart/form-data; boundary=%@”, stringBoundary];
    [urlRequest addValue:contentType forHTTPHeaderField:@”Content-Type”];

    NSLog(@”Start POST”);
    // Setting up the POST request’s multipart/form-data body
    postBody = [NSMutableData data];
    [postBody appendData:[[NSString stringWithFormat:@”rnrn–%@rn”, stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@”Content-Disposition: form-data; name=”source”rnrn”] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@”lighttable”] dataUsingEncoding:NSUTF8StringEncoding]]; // So Light Table show up as source in Twitter post

    [postBody appendData:[[NSString stringWithFormat:@”rn–%@rn”, stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@”Content-Disposition: form-data; name=”media”; filename=”%@”rn”, @”lighttable_twitpic_image.jpg” ] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@”Content-Type: image/pngrn”] dataUsingEncoding:NSUTF8StringEncoding]]; // jpeg as data
    [postBody appendData:[[NSString stringWithString:@”Content-Transfer-Encoding: binaryrnrn”] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:imageData]; // Tack on the imageData to the end

    [postBody appendData:[[NSString stringWithFormat:@”rn–%@rn”, stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@”Content-Disposition: form-data; name=”username”rnrn”] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:TwitterUN] dataUsingEncoding:NSUTF8StringEncoding]]; // username

    [postBody appendData:[[NSString stringWithFormat:@”rn–%@rn”, stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@”Content-Disposition: form-data; name=”password”rnrn”] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:TwitterPW] dataUsingEncoding:NSUTF8StringEncoding]]; // password

    [postBody appendData:[[NSString stringWithFormat:@”rn–%@rn”, stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@”Content-Disposition: form-data; name=”message”rnrn”] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:message] dataUsingEncoding:NSUTF8StringEncoding]]; // message

    [postBody appendData:[[NSString stringWithFormat:@”rn–%@–rn”, stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [urlRequest setHTTPBody:postBody];

    NSLog(@”POST sent”);