2014-03-26 97 views
0

我想要使用下面的代碼從給定的用戶名請求和解析20條推文。儘管在NSLog中,我得到的只是錯誤的身份驗證錯誤。有人可以幫助我修正我的代碼並指出我朝着正確的方向嗎?要求使用Twitter API的用戶名的Twitter時間線1.1

在此先感謝!您被授予訪問權限之前

- (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 
     } 


    }];} 

回答

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 
       } 
      }]; 
     } 
    }]; 
} 
+0

謝謝,但仍然得到錯誤認證錯誤。 –

+0

它適用於我/我的用戶名。嘗試調用[self getTweets:@「IsaRanjha」](沒有第二個@)。確保這是您的實際用戶名。 – Sean

+0

當我回家時,我會嘗試。它在日誌中究竟報告了什麼,只有最後的50條推文是正確的?我將如何將這個信息加載到UITableView中?因爲我所知道的是用數組加載UITableView,你能幫我嗎?謝謝。 –

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"); 
      } 
     }]; 
} 

看我怎麼打發PARAMS的API

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

謝謝,但是當我跑步,沒有得到任何東西。 –

+0

好吧,快速瀏覽一下,只有當您的Twitter應用程序使用有效帳戶登錄時,Twitter API纔會工作 –