【问题标题】:REQUEST Twitter timeline for username using Twitter API 1.1使用 Twitter API 1.1 请求用户名的 Twitter 时间线
【发布时间】:2014-05-04 06:05:08
【问题描述】:

我正在尝试使用下面的代码从给定的用户名请求和解析 20 条推文。虽然,在 NSLog 中,我得到的只是错误的身份验证错误。有人可以帮我修复我的代码并指出正确的方向吗?

提前致谢!

- (void)getTweets:(NSString *)username {

    NSURL *getTimeline = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/user_timeline.json"];

    NSString *paraString = [NSString stringWithFormat:@"%@", username];
    NSDictionary *parameters = [NSDictionary dictionaryWithObject:paraString forKey:@"username"];

    SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:getTimeline parameters:parameters];

    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {

        if (granted) {
            NSArray *accounts = [accountStore accountsWithAccountType:accountType];
            if (accounts.count > 0)
            {
                for (ACAccount *twitterAccount in accounts) {
                    [request setAccount:twitterAccount];

                }
            }

            [self getTweets:@"@IsaRanjha"];
        }
    }
      ];

    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
         if (!error)
         {
             NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
             NSLog(@"%@", json);

         }
         else
         {
             //Deal with error
         }


     }];}

【问题讨论】:

    标签: ios json twitter


    【解决方案1】:

    在您被授予访问权限之前,您的 performRequestWithHandler 正在触发。像这样将它添加到 requestAccessToAccountsWithType 完成块中。您也不应该需要从自身内部调用相同的函数 [self getTweets:@"@IsaRanjha"]。在 viewDidLoad 中尝试一下。

    - (void)getTweets:(NSString *)username {
    
        NSURL *getTimeline = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/user_timeline.json"];
    
        NSString *paraString = [NSString stringWithFormat:@"%@", username];
        NSDictionary *parameters = [NSDictionary dictionaryWithObject:paraString forKey:@"seansamocki"];
    
        SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:getTimeline parameters:parameters];
    
        ACAccountStore *accountStore = [[ACAccountStore alloc] init];
        ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
        [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
    
            if (granted) {
                NSArray *accounts = [accountStore accountsWithAccountType:accountType];
                if (accounts.count > 0)
                {
                    for (ACAccount *twitterAccount in accounts) {
                        [request setAccount:twitterAccount];
    
                    }
                }
                    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                    if (!error)
                    {
                        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
                        NSLog(@"%@", json);
    
                    }
                    else
                    {
                        //Deal with error
                    }
                }];
            }
        }];
    }
    

    【讨论】:

    • 谢谢,但仍然收到错误的身份验证错误。
    • 它对我/我的用户名有用。尝试调用 [self getTweets:@"IsaRanjha"] (没有第二个 @)。确保这是您的实际用户名。
    • 我回家后试试。它在日志中究竟报告了什么,只是最后 50 条推文,对吗?我如何将这些信息加载到 UITableView 中?因为我所知道的只是加载一个带有数组的 UITableView,你能帮我吗?谢谢。
    • 试过了,没有运气。同样的错误。错误=({代码= 215;消息=“错误的身份验证数据”;}); }
    • 这个账号是否在设置中添加到你的iOS设备/模拟器中?
    【解决方案2】:

    试试这个

    -(void)getTweets
    {
    
            ACAccountStore *accountStore = [[ACAccountStore alloc] init];
            ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    
            [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error){
                if (granted) {
    
                    NSArray *accounts = [accountStore accountsWithAccountType:accountType];
                    if (accounts.count > 0)
                    {
                        ACAccount *twitterAccount = [accounts objectAtIndex:0];
    
                        // Creating a request to get the info about a user on Twitter
    
                        NSURL* url = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/user_timeline.json"];
                        NSDictionary* params = @{@"count" : @"50", @"screen_name" : @"IsaRanjha"};
    
                        SLRequest *twitterInfoRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter
                                                                           requestMethod:SLRequestMethodGET
                                                                                     URL:url parameters:params];
                        [twitterInfoRequest setAccount:twitterAccount];
                        [twitterInfoRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                            dispatch_async(dispatch_get_main_queue(), ^{
    
                                if ([urlResponse statusCode] == 429) {
                                    NSLog(@"Rate limit reached");
                                    return;
                                }
                                if (error) {
                                    NSLog(@"Error: %@", error.localizedDescription);
                                    return;
                                }
                                if (responseData) {
                                    NSError *jsonError;
                                    self.twitterFeedsArray = [NSJSONSerialization
                                              JSONObjectWithData:responseData
                                              options:NSJSONReadingAllowFragments
                                              error:&jsonError];
                                    dispatch_async(dispatch_get_main_queue(), ^{
                                        NSLog(@"%@",@"Do");
                                        [[NSNotificationCenter defaultCenter]postNotificationName:TwitterGotPepsiTweets object:nil];
                                    });
                                }
                            });
                        }];
                    }
                } else {
                    NSLog(@"No access granted");
                }
            }];
    }
    

    看看我如何为 api 传递参数

    NSDictionary* params = @{@"count" : @"50", @"screen_name" : @"IsaRanjha"};
    

    【讨论】:

    • 谢谢,但是当我运行时,什么都没有。
    • 好吧,快速浏览一下,只有当您使用有效帐户登录 twitter 应用程序时,twitter api 才能工作
    猜你喜欢
    • 2013-06-08
    • 1970-01-01
    • 2013-03-30
    • 2013-12-19
    • 1970-01-01
    • 1970-01-01
    • 2017-12-30
    • 2013-12-11
    • 2013-07-22
    相关资源
    最近更新 更多