在 iPhone 上使用 Twitter+OAuth 进行推文搜索
最初我需要能够在 Twitter 上使用搜索 API。我使用 Matt Gemmell 出色的 MGTwitterEngine 来完成此操作。该代码非常非常简单,看起来像这样:
- (void)viewDidLoad {
[super viewDidLoad];
tweetArrays = nil;
tweetNameArray = nil;
NSString *username = @"<username>";
NSString *password = @"<password>";
NSString *consumerKey = @"<consumerKey>";
NSString *consumerSecret = @"<consumerSecret>";
// Most API calls require a name and password to be set...
if (! username || ! password || !consumerKey || !consumerSecret) {
NSLog(@"You forgot to specify your username/password/key/secret in AppController.m, things might not work!");
NSLog(@"And if things are mysteriously working without the username/password, it's because NSURLConnection is using a session cookie from another connection.");
}
// Create a TwitterEngine and set our login details.
twitterEngine = [[MGTwitterEngine alloc] initWithDelegate:self];
[twitterEngine setUsesSecureConnection:NO];
[twitterEngine setConsumerKey:consumerKey secret:consumerSecret];
// This has been undepreciated for the purposes of dealing with Lists.
// At present the list API calls require you to specify a user that owns the list.
[twitterEngine setUsername:username];
[twitterEngine getSearchResultsForQuery:@"#HelloWorld" sinceID:0 startingAtPage:1 count:100];
}
这最终会调用该函数:
- (void)searchResultsReceived:(NSArray *)searchResults forRequest:(NSString *)connectionIdentifier
然后我可以对 searchResults 执行我想要的操作。这要求我包含 yajl 库。
然后我想扩展我的代码以允许用户发推文。我下载了 Ben Gottlieb 的优秀代码 Twitter-OAuth-iPhone
所以只有一个问题。 getSearchResultsForQuery 返回 requestFailed 并出现以下错误:
Error Domain=HTTP Code=400 "The operation couldn’t be completed. (HTTP error 400.)"
要调用此代码,我只需在 Twitter-OAuth-iPhone 中获取演示项目,并添加对 getSearchResultsForQuery 的调用,如下所示:
- (void) viewDidAppear: (BOOL)animated {
if (_engine) return;
_engine = [[SA_OAuthTwitterEngine alloc] initOAuthWithDelegate: self];
_engine.consumerKey = kOAuthConsumerKey;
_engine.consumerSecret = kOAuthConsumerSecret;
UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine: _engine delegate: self];
if (controller)
[self presentModalViewController: controller animated: YES];
else {
[_engine getSearchResultsForQuery:@"HelloWorld"];
// [_engine sendUpdate: [NSString stringWithFormat: @"Already Updated. %@", [NSDate date]]];
}
}
如上所述,这会返回 400 错误。我在此处添加的所有其他 Twitter API 调用都可以正常工作,例如:
- (NSString *)getRepliesStartingAtPage:(int)pageNum;
我做错了什么吗?或者 getSearchResultsForQuery 不再起作用?这两个代码库似乎使用不同版本的 MGTwitterEngine,这可能是导致问题的原因吗?
谢谢!
Originally I needed the ability to use the search API with twitter. I did this using Matt Gemmell's great MGTwitterEngine. That code was very very simple and looked something like this:
- (void)viewDidLoad {
[super viewDidLoad];
tweetArrays = nil;
tweetNameArray = nil;
NSString *username = @"<username>";
NSString *password = @"<password>";
NSString *consumerKey = @"<consumerKey>";
NSString *consumerSecret = @"<consumerSecret>";
// Most API calls require a name and password to be set...
if (! username || ! password || !consumerKey || !consumerSecret) {
NSLog(@"You forgot to specify your username/password/key/secret in AppController.m, things might not work!");
NSLog(@"And if things are mysteriously working without the username/password, it's because NSURLConnection is using a session cookie from another connection.");
}
// Create a TwitterEngine and set our login details.
twitterEngine = [[MGTwitterEngine alloc] initWithDelegate:self];
[twitterEngine setUsesSecureConnection:NO];
[twitterEngine setConsumerKey:consumerKey secret:consumerSecret];
// This has been undepreciated for the purposes of dealing with Lists.
// At present the list API calls require you to specify a user that owns the list.
[twitterEngine setUsername:username];
[twitterEngine getSearchResultsForQuery:@"#HelloWorld" sinceID:0 startingAtPage:1 count:100];
}
This would end up calling the function:
- (void)searchResultsReceived:(NSArray *)searchResults forRequest:(NSString *)connectionIdentifier
And then I could do what I wanted with the searchResults. This required me to include the yajl library.
I then wanted to expand my code to allow users to tweet. I downloaded Ben Gottlieb's great code Twitter-OAuth-iPhone
So there's only one problem. The getSearchResultsForQuery returns a requestFailed with the following error:
Error Domain=HTTP Code=400 "The operation couldn’t be completed. (HTTP error 400.)"
To call this code I simply took the demo project in Twitter-OAuth-iPhone and added a call to getSearchResultsForQuery as seen here:
- (void) viewDidAppear: (BOOL)animated {
if (_engine) return;
_engine = [[SA_OAuthTwitterEngine alloc] initOAuthWithDelegate: self];
_engine.consumerKey = kOAuthConsumerKey;
_engine.consumerSecret = kOAuthConsumerSecret;
UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine: _engine delegate: self];
if (controller)
[self presentModalViewController: controller animated: YES];
else {
[_engine getSearchResultsForQuery:@"HelloWorld"];
// [_engine sendUpdate: [NSString stringWithFormat: @"Already Updated. %@", [NSDate date]]];
}
}
This as stated above returns a 400 error. Every other twitter API call I add here does work such as:
- (NSString *)getRepliesStartingAtPage:(int)pageNum;
Am I doing anything wrong? Or does getSearchResultsForQuery no longer work? The two code bases seem to use different versions of MGTwitterEngine, could that be causing the problem?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您必须将 twitter 引擎实例化为 SA_OAuthTwitterEngine 的实例,而不是 MGTwitterEngine。当您调用 getSearchResultsForQuery 时,它会使用对 _sendRequestWithMethod 的调用来实际发送搜索请求。 SA_OAuthTwitterEngine 覆盖 MGTwitterEngine 的实现,但其该函数的版本不适用于 getSearchResultsForQuery。
因此,您需要转到 getSearchResultsForQuery 并使其使用 MGTwitterEngine 的函数版本。
The problem is that you have to instantiate the twitter engine as an instance of SA_OAuthTwitterEngine, instead of MGTwitterEngine. When you call getSearchResultsForQuery, it uses a call to _sendRequestWithMethod to actually send the search request. SA_OAuthTwitterEngine overrides the MGTwitterEngine's implementation, but its version of that function doesn't work with getSearchResultsForQuery.
So, you need to go to getSearchResultsForQuery and make it use the MGTwitterEngine's version of the function.