如何在iOS中使用oauth在Twitter帐户中发布图像?

时间:2013-12-02 12:10:28

标签: ios image post twitter twitter-oauth

我已经设法接收了如下的Twitter oauth数据(在用户登录后),并且尽管我已经搜索过,但我找不到一种方法可以正常运行发布图像在用户的推特个人资料中。有没有详细的教程?

  oauth_token=xxxxxxxxjU8gs6&oauth_token_secret=xxxxD7ITa&user_id=xxx73&screen_name=manostijeras&oauth_verifier=xx07

由于

PS:我使用本教程接收twitter oauth数据 http://www.icodeblog.com/2010/09/16/dealing-with-the-twitter-oauth-apocalypse/

1 个答案:

答案 0 :(得分:3)

这是程序 - >

OAToken *token = [[OAToken alloc] initWithKey:OauthAccessToken secret:OauthAccessSecrateKey]; //Set user Oauth access token and secrate key
OAConsumer *consumer = [[OAConsumer alloc] initWithKey:ConsumerToken secret:ConsumerSecrateKey]; // Application cosumer token and secrate key

// Url for upload pictures
NSURL *finalURL = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update_with_media.json];

OAMutableURLRequest *theRequest = [[OAMutableURLRequest alloc] initWithURL:finalURL
                                                                   consumer:consumer
                                                                      token:token
                                                                      realm: nil
                                                          signatureProvider:nil];

[theRequest setHTTPMethod:@"POST"];
[theRequest setTimeoutInterval:120];
[theRequest setHTTPShouldHandleCookies:NO];

// Set headers for client information, for tracking purposes at Twitter.(This is optional)
[theRequest setValue:@"TestIphone" forHTTPHeaderField:@"X-Twitter-Client"];
[theRequest setValue:@"1.0" forHTTPHeaderField:@"X-Twitter-Client-Version"];
[theRequest setValue:@"http://www.TestIphone.com/" forHTTPHeaderField:@"X-Twitter-Client-URL"];

NSString *boundary = @"--Hi all, First Share"; // example taken and implemented.
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[theRequest setValue:contentType forHTTPHeaderField:@"content-type"];

NSMutableData *body = [NSMutableData data];

[body appendData:[[NSString stringWithFormat:@"--%@\r\n\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"status\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@",@"latest upload"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"media[]\"; filename=\"vizllx_pic1.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:UIImageJPEGRepresentation([UIImage imageNamed:@"vizllx_pic.png"], 0.5)]];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[theRequest prepare];

NSString *oAuthHeader = [theRequest valueForHTTPHeaderField:@"Authorization"];
[theRequest setHTTPBody:body];

NSHTTPURLResponse *response = nil;
NSError *error = nil;

NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest
                                             returningResponse:&response                            
                                                         error:&error];
NSString *responseString = [[NSString alloc] initWithData:responseData                                
                                                 encoding:NSUTF8StringEncoding];