如何通过twitter api发布推文?

时间:2011-12-15 05:23:25

标签: iphone twitter httprequest twitter-oauth

我已将twitter api集成到我的应用程序中。在我的应用程序中,我使用oauth库获得了oauth_token和oauth_secre_key。不,我想发布一条新推文。我如何使用授权标头发送我的http请求。我的问题是我有一个来自twitter api的发送消息的URL是:-https://api.twitter.com/1/statuses/update.json?status = Maybe%20he%27ll%20finally%20find%20his% 20键。%20%23peterfalk& trim_user = true& include_entities = true但是当我打开浏览器然后我收到此错误: -

  

{“错误”:“无法验证您。”,“请求”:“/ 1 / statuses / update.json?status =可能%20he%27ll%20finally%20find%20his%20keys。%20%23peterfalk& ; trim_user =真安培; include_entities =真“}

修复此错误的方法是什么?我如何在我的请求中添加授权标题?

谢谢你的进步......

1 个答案:

答案 0 :(得分:1)

我收到此错误,因为我没有以正确的格式设置参数。现在我使用此代码并正常工作。

  

NSString * trimmedText = [NSString stringWithFormat:@“%@”,txt_comment.text];

NSURL *url=[NSURL URLWithString: @"https://api.twitter.com/1/statuses/update.xml"];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
[params setObject:trimmedText forKey:@"status"];
NSString *body = [self _queryStringWithBase:nil parameters:params prefixed:NO];

NSData* requestData = [body dataUsingEncoding: NSUTF8StringEncoding];


OAMutableURLRequest *request = [[[OAMutableURLRequest alloc] initWithURL:url consumer: appDelegate.consumer token: appDelegate.accessToken realm: nil signatureProvider: nil] autorelease];  

[request setHTTPMethod:@"POST"];
[request setHTTPBody:requestData];
OADataFetcher *fetcher = [[OADataFetcher alloc] init];

[fetcher fetchDataWithRequest:request
                     delegate:self
            didFinishSelector:@selector(accessTokenTicket_post:didFinishWithData:)
              didFailSelector:@selector(accessTokenTicket:didFailWithError:)];