【问题标题】:Using tweet search with Twitter+OAuth on iPhone在 iPhone 上通过 Twitter+OAuth 使用推文搜索
【发布时间】:2011-08-29 07:37:46
【问题描述】:

最初我需要能够将搜索 API 与 twitter 一起使用。我使用 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,这会导致问题吗?

谢谢!

【问题讨论】:

    标签: iphone oauth twitter twitter-oauth


    【解决方案1】:

    问题是您必须将 twitter 引擎实例化为 SA_OAuthTwitterEngine 的实例,而不是 MGTwitterEngine。当您调用 getSearchResultsForQuery 时,它使用对 _sendRequestWithMethod 的调用来实际发送搜索请求。 SA_OAuthTwitterEngine 覆盖了 MGTwitterEngine 的实现,但该函数的版本不适用于 getSearchResultsForQuery。

    所以,你需要去getSearchResultsForQuery,让它使用MGTwitterEngine版本的函数。

    【讨论】:

      猜你喜欢
      • 2013-05-30
      • 2013-09-17
      • 2012-10-04
      • 1970-01-01
      • 2012-09-29
      • 2014-06-30
      • 1970-01-01
      • 2013-08-19
      • 2021-07-25
      相关资源
      最近更新 更多