iPhone-Twitter API 获取用户关注者/正在关注

2023-11-22

我希望能够使用适用于 ios 5 的 Twitter API 将所有用户关注者和关注用户名放入NSDictionary...

但我遇到了障碍。我不知道如何使用 Twitter API 来执行此操作...但我的主要问题是首先获取用户的用户名。当我什至不知道用户的用户名时,如何发出 API 请求来查找该用户的关注者?

有人能给我举个关于获取 Twitter 用户关注者和关注者的例子吗?

PS:我已经添加了Twitter框架,并导入


它是 Apple 的 Twitter API 和 Twitter 自己的 API 的组合。一旦你阅读了代码,它就相当简单了。我将提供有关如何获取 Twitter 帐户的“朋友”(这是用户关注的人的术语)的示例代码,这应该足以让您继续了解获取 Twitter 帐户的关注者的方法。帐户。

首先,添加Accounts and Twitter构架。

现在,让我们获取用户设备上存在的 Twitter 帐户。

#import <Accounts/Accounts.h>

-(void)getTwitterAccounts {
    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    // Create an account type that ensures Twitter accounts are retrieved.
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    // let's request access and fetch the accounts
    [accountStore requestAccessToAccountsWithType:accountType
                            withCompletionHandler:^(BOOL granted, NSError *error) {
                                // check that the user granted us access and there were no errors (such as no accounts added on the users device)
                                if (granted && !error) {
                                    NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
                                    if ([accountsArray count] > 1) {
                                        // a user may have one or more accounts added to their device
                                        // you need to either show a prompt or a separate view to have a user select the account(s) you need to get the followers and friends for 
                                    } else {
                                        [self getTwitterFriendsForAccount:[accountsArray objectAtIndex:0]];
                                    }
                                } else {
                                    // handle error (show alert with information that the user has not granted your app access, etc.)
                                }
    }];
}

现在我们可以使用以下命令让朋友注册一个帐户获取好友/ID命令:

#import <Twitter/Twitter.h>

-(void)getTwitterFriendsForAccount:(ACAccount*)account {
    // In this case I am creating a dictionary for the account
    // Add the account screen name
    NSMutableDictionary *accountDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:account.username, @"screen_name", nil];
    // Add the user id (I needed it in my case, but it's not necessary for doing the requests)
    [accountDictionary setObject:[[[account dictionaryWithValuesForKeys:[NSArray arrayWithObject:@"properties"]] objectForKey:@"properties"] objectForKey:@"user_id"] forKey:@"user_id"];
    // Setup the URL, as you can see it's just Twitter's own API url scheme. In this case we want to receive it in JSON
    NSURL *followingURL = [NSURL URLWithString:@"http://api.twitter.com/1/friends/ids.json"];
    // Pass in the parameters (basically '.ids.json?screen_name=[screen_name]')
    NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:account.username, @"screen_name", nil];
    // Setup the request
    TWRequest *twitterRequest = [[TWRequest alloc] initWithURL:followingURL
                                                parameters:parameters
                                             requestMethod:TWRequestMethodGET];
    // This is important! Set the account for the request so we can do an authenticated request. Without this you cannot get the followers for private accounts and Twitter may also return an error if you're doing too many requests
    [twitterRequest setAccount:account];
    // Perform the request for Twitter friends
    [twitterRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                if (error) {
                    // deal with any errors - keep in mind, though you may receive a valid response that contains an error, so you may want to look at the response and ensure no 'error:' key is present in the dictionary
                }
                NSError *jsonError = nil;
                // Convert the response into a dictionary
                NSDictionary *twitterFriends = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONWritingPrettyPrinted error:&jsonError];
                // Grab the Ids that Twitter returned and add them to the dictionary we created earlier
                [accountDictionary setObject:[twitterFriends objectForKey:@"ids"] forKey:@"friends_ids"];
                NSLog(@"%@", accountDictionary);
    }];
}

当您想要一个帐户的关注者时,几乎是一样的......简单使用 URLhttp://api.twitter.com/1/followers/ids.format并传入通过找到的所需参数获取关注者/id

希望这能给您一个良好的开端。

UPDATE:

正如评论中指出的,您应该使用更新后的 API 调用:https://api.twitter.com/1.1/followers/list.json

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

iPhone-Twitter API 获取用户关注者/正在关注 的相关文章

随机推荐

  • php:file_get_contents编码问题

    我的任务很简单 向translate google com 发出发布请求并获取翻译 在下面的示例中 我使用 hello 一词翻译成俄语 header Content Type text plain charset utf 8 optiona
  • 在不使用 Ghostscript 且仅使用 ImageMagik 的情况下压缩文本较多的 PDF 会导致文本模糊

    我正在尝试使用代码压缩校报的 PDF 版本 并创建了以下完美运行的脚本 gs dNOPAUSE dBATCH sDEVICE pdfwrite dCompatibilityLevel 1 4 dPDFSETTINGS ebook sOutp
  • 在目标 c 中将数字转换为单词

    我需要写下这样的数字 1 gt 耶克 123 gt 我的朋友 1 123 gt 耶克 赫扎尔 耶克萨德 比斯特 奥塞 3 002 001 gt Se milion o do hezar o yek 我是目标 c 的初学者 我用 c 编写它
  • java for 循环不工作

    我希望这不是一个愚蠢的问题 但我已经查找了我能找到的每个示例 但似乎我的代码仍然正确 但它仍然无法正常工作 我输入一个数字 然后它会转到下一个数字一行代码而不是循环 我用它来用用户输入的数字填充数组 我很感激任何帮助 谢谢 for i 0
  • 如何使用 talend 和 sql server 更快地加载数据

    我使用 Talend 将数据加载到 sql server 数据库中 看来我的工作最薄弱的地方不是数据处理 而是数据库中的有效负载 其速度不超过 17 行 秒 有趣的是 我可以同时启动 5 个作业 它们都将以 17 行 秒的速度加载 什么可以
  • Double.ToString() 何时返回科学计数形式的值?

    我认为它与前导零或尾随零的数量有关 但我在 msdn 中找不到任何可以给我具体答案的内容 在什么时候Double ToString CultureInfo InvariantCulture 开始以科学记数法返回值 从文档中Double To
  • 使用 IOS 8.3 的 Safari 上的 Angular ng-click 问题

    这是一个奇怪的问题 很难产生和探索 在使用 Angular 构建 Web 应用程序时 我的老板发现应用程序上的所有按钮都使用ng click指令不起作用 现在 这个问题只发生在iphone 6 with IOS 8 3 and using
  • 使用nodejs gcloud api移动/重命名Google Cloud Storage中的文件夹

    我正在尝试使用 gcloud api 重命名或移动 google 云存储中的文件夹 类似的问题解释了如何删除文件夹 使用nodejs gcloud api删除Google Cloud Storage中的文件夹 但是如何重命名文件夹呢 或转向
  • 使用C#从USB读取数据?

    我不想读取串行端口或其他可能的简单快捷方式 我想知道如何使用 C 读取笔记本电脑中的 USB 端口 无论您是否可以推荐一个网站或解释一下流程的流程 我将非常感谢您的帮助 如果你想做 USB 开发 Jan Axelson 的 USB Comp
  • Combobox Cascading 需要更具体的cascadeFrom 选项

    我想使用 Kendo UI ComboBox 的 CascadeFrom 功能 但令我沮丧的是 该选项似乎只接受 ID 现在我无法使用该 ID 因为组合框是动态添加的 并且可能多次添加 从而导致多个控件具有相同的 ID 有谁知道如何将特定的
  • hprof 中“无引用”的对象

    我正在调查一个hprofVisualVM 中的文件 服务器运行 JDK 1 4 2 30 并具有 1 GB 堆 NewSize 为 200 Mb hprof 显示 56000 个实例占用了 71 的堆int 在 VisualVM 中查看时
  • 现在允许使用 Instagram API 上传视频吗?

    据 Instagram 报道文档 Instagram 仅允许 API 上传图像 JPG PNG 一款名为 Cinamatic 的新 iPhone 应用程序允许用户通过 Cinamatic 登录 Instagram 后将视频上传到 Insta
  • Android Studio w/gradle:包 r 不存在

    最终编辑 这是一个很长的问题 需要进行多次编辑 而且我不一定在每一步都做正确的事情 对于任何偶然发现这个问题并且在 Android Studio 中将项目转换为 gradle 时遇到困难的人 我建议检查一下这个存储库中提到的问题 16718
  • 如何让 Powermock 与 Dexmaker 配合使用

    我正在尝试合并动力模拟使用以下 build gradle 配置作为我的 Android 测试的依赖项 dependencies compile com android support appcompat v7 21 0 androidTes
  • `exec':字符串包含空字节(ArgumentError)

    cmd snv co rep username svn user password pxs puts cmd this code wotks and prints all vars values normally exec cmd xpto
  • 无法为 Android 教程构建 GStreamer

    我在尝试构建 GStreamer Android 教程时遇到了许多问题 我的环境是 Mac OS X 7 Android SDK 版本 17 安卓NDK 8d 我能够在 Eclipse 中和命令行中构建和运行 NDK 示例 我已经下载了ht
  • YAML 预处理器/宏处理器

    有没有一种简单的方法可以将预处理器 宏处理器与 YAML 文件一起使用 即我正在考虑类似于 C 预处理器的东西 我们有很多描述各种数据结构的平面文本文件 它们目前采用我们自己的内部格式 并使用内部解析器读取 我想切换到 YAML 文件以利用
  • SQLCLR 和 DateTime2

    使用 SQL Server 2008 Visual Studio 2005 net 2 0 SP2 支持新的 SQL Server 2008 数据类型 我正在尝试编写一个 SQLCLR 函数 该函数将 DateTime2 作为输入并返回另一
  • 如何正确处理UnsafeMutablePointer

    我有点困惑 我什么时候必须调用 free 以及何时 destroy dealloc 我正在编写一个学习核心音频的简短代码片段 我想如果我打电话UnsafeMutablePointer
  • iPhone-Twitter API 获取用户关注者/正在关注

    我希望能够使用适用于 ios 5 的 Twitter API 将所有用户关注者和关注用户名放入NSDictionary 但我遇到了障碍 我不知道如何使用 Twitter API 来执行此操作 但我的主要问题是首先获取用户的用户名 当我什至不