【问题标题】:iOS: get Twitter feed of public accountiOS:获取公众账号的 Twitter 提要
【发布时间】:2015-11-25 21:50:43
【问题描述】:

我需要获取公共帐户的 Twitter 提要。我可以使用以下代码获取自己的 Twitter 提要:

-(void) testTwitter {
{
    ACAccountStore *accountStore = [[ACAccountStore alloc]init];

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

                     //Succesful Access
                     NSArray *twitterAccounts = [accountStore accountsWithAccountType:accountType];
                     if (twitterAccounts != nil)
                     {
                         ACAccount *currentAccount = [twitterAccounts objectAtIndex:0];
                         if (currentAccount != nil)
                         {
                             NSString *paraString = [NSString stringWithFormat:@"qlx_corp"];
                             NSDictionary *parameters = [NSDictionary dictionaryWithObject:paraString forKey:@"username"];

                             NSString *requestString = @"https://api.twitter.com/1.1/statuses/user_timeline.json";
                             SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:[NSURL URLWithString:requestString] parameters:parameters];
                             [request setAccount:currentAccount];

                             [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
                              {
                                  if ((error == nil) && ([urlResponse statusCode] == 200))
                                  {
                                      NSArray *twitterFeed = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
                                      NSDictionary *firstPost = [twitterFeed objectAtIndex:0];

                                      NSLog(@"firstPost = %@", [firstPost description]);
                                  }
                                  else {
                                      NSLog(@"error: %@",error);
                                  }
                              }];
                         }
                     }
                 }
                 else
                 {
                     //Access Denied
                     NSLog(@"access denied");
                 }
             }];
        }
    }

}
}

但是当我用下面的代码代替我自己的另一个帐户时,我得到了这个错误:

代码 = 215;

message = "错误的身份验证数据。";

这是我替换成功访问的代码(在已发布代码的 cmets 中注明):

NSURL* url = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/user_timeline.json"];
                     NSDictionary* params = @{@"count" : @"10", @"screen_name" : @"qlx_corp"};

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

                     [request performRequestWithHandler:^(NSData *responseData,
                                                          NSHTTPURLResponse *urlResponse, NSError *error) {

                         if (!error) {
                             NSError *jsonError;
                         NSArray *responseJSON = [NSJSONSerialization
                                                  JSONObjectWithData:responseData
                                                  options:NSJSONReadingAllowFragments
                                                  error:&jsonError];
                             NSLog(@"response json: %@",responseJSON);
                         // Show the content of the responseJSON in a view.
                     }
                      }];

我做错了什么,我收到此错误消息,而不是我请求的公共 Twitter 提要。我的应用请求使用 Twitter 的权限,并且已获得批准,那么可能是什么问题?

【问题讨论】:

  • 好吧,我没有使用 Twitter API,我使用的是 Apple 的社交框架。所以我试图避免使用不同的技术,但如果我找不到解决方案,那将是寻找替代方案的好地方。谢谢!

标签: ios objective-c xcode twitter


【解决方案1】:

我最终使用了 Twitter API,因为现在它们支持仅应用程序模式,不需要用户身份验证即可显示公共 Twitter 提要。有关更多信息,请参阅此链接:Twitter OAuth and accessToken to GET statuses/user_timeline in Objective-C

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-06
    • 1970-01-01
    • 2015-04-14
    • 1970-01-01
    • 2015-11-18
    • 2013-06-14
    • 2013-09-02
    • 2012-06-24
    相关资源
    最近更新 更多