开发者

Integrating twitpic OAuth for iPhone

How can I integrate twitpic API with OAuth for posting an image from iPhone? Any help or tutorial? Currently I am doing...

        NSURL *twitpicURL = [NSURL URLWithString:@"http://api.twitpic.com/2/upload.format"];
    theRequest = [NSMutableURLRequest requestWithURL:twitpicURL];
    [theRequest setHTTPMethod:@"POST"];

    // Set the params
    NSString *message = theMessage;

    [theRequest addValue:@"http://api.twitter.com/" forHTTPHeaderField:@"OAuth realm"];
    [theRequest addValue:TWITPIC_API_KEY forHTTPHeaderField:@"oauth_consumer_key"];
    [theRequest addValue:@"HMAC-SHA1" forHTTPHeaderField:@"oauth_signature_method"];
    [theRequest addValue:USER_OAUTH_TOKEN forHTTPHeaderField:@"oauth_token"];
    [theRequest addValue:USER_OAUTH_SECRET forHTTPHeaderField:@"oauth_secret"];
    [theRequest addValue: @"1272325550" forHTTPHeaderField:@"oauth_timestamp"];
    [theRequest addValue:nil forHTTPHeaderField:@"oauth_nonce"];
    [theRequest addValue:@"1.0" forHTTPHeaderField:@"oauth_version"];
    [theRequest addValue:nil forHTTPHeaderField:@"oauth_signature"];

    NSMutableData *postBody = [NSMutableData data];
    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"source\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"lighttable"] dataUsingEncoding:NSUTF8StringEncoding]];

    // Message
    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"message\"\r\n\r\n%@", message]dataUsingEncoding:NSUTF8StringEncoding]];

    // Media
    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"media\"; filename=\"%@\"\r\n", @"doc_twitpic_image.jpg"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWi开发者_如何学编程thFormat:@"Content-Type: image/jpg\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; // data as JPEG

    [postBody appendData:[[NSString stringWithFormat:@"Content-Transfer-Encoding: binary\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[NSData dataWithData:image]];

    [theRequest setHTTPBody:postBody];
    [theRequest setValue:[NSString stringWithFormat:@"%d", [postBody length]] forHTTPHeaderField:@"Content-Length"];    
    theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];


Use GSTwitPicEngine for iPhone:

http://github.com/Gurpartap/GSTwitPicEngine


Have a look at the OAuthConsumer library.

If you decide to use it, you'll have to compile it as a static library or simply include the headers (see here for an explanation).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜