如何通过 twitter api 发布推文?

发布于 2024-12-21 05:21:20 字数 593 浏览 1 评论 0原文

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

{“错误”:“无法验证您的身份。”,“请求”:“/1/statuses/update.json?status=Maybe%20he%27ll%20finally%20find%20his%20keys.%20%23peterfalk& ;trim_user=true&include_entities=true"}

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

预先感谢...

I have integrate twitter api in my application. In my application i have got oauth_token and oauth_secre_key by using oauth library. NOw i want to post a new tweet. How i send my http request with authorization header. my problem is that i have a url for send message from twitter api that is :-https://api.twitter.com/1/statuses/update.json?status=Maybe%20he%27ll%20finally%20find%20his%20keys.%20%23peterfalk&trim_user=true&include_entities=true but when i open i browser then i got this error:-

{"error":"Could not authenticate you.","request":"/1/statuses/update.json?status=Maybe%20he%27ll%20finally%20find%20his%20keys.%20%23peterfalk&trim_user=true&include_entities=true"}

What is the way to fix this error? How i add authorization header with my request?

Thanks in advances...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光磨忆 2024-12-28 05:21:20

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

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:)];

I get this error because i have not set parameter in proper format. Now i use this code and working fine.

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:)];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文