首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用iOS中的oauth在twitter帐户中发布图像?

如何使用iOS中的oauth在twitter帐户中发布图像?
EN

Stack Overflow用户
提问于 2013-12-02 12:10:27
回答 1查看 444关注 0票数 2

我已经成功地收到了下面的twitter数据(在用户登录之后),尽管我已经搜索了很多,但我还没有找到一种方法可以很好地在用户的twitter配置文件中发布图像。有详细的教程吗?

代码语言:javascript
复制
  oauth_token=xxxxxxxxjU8gs6&oauth_token_secret=xxxxD7ITa&user_id=xxx73&screen_name=manostijeras&oauth_verifier=xx07

谢谢

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-02 12:17:07

这是程序->

代码语言:javascript
复制
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];
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20328001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档