使用Twitter API 1.1请求用户名的Twitter时间线

时间:2014-03-26 03:14:41

标签: ios json twitter

我正在尝试使用下面的代码请求并解析来自给定用户名的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
         }


     }];}

2 个答案:

答案 0 :(得分:0)

在授予访问权限之前,您的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
                }
            }];
        }
    }];
}

答案 1 :(得分:0)

好吧试试这个

-(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的params

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