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

时间:2023-03-23
本文介绍了iPhone- Twitter API 获取用户关注者/关注者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我希望能够使用 iOS 5 的 Twitter API 来获取所有用户关注者和关注用户名到 NSDictionary...

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

谁能给我一个关于让你的 Twitter 用户关注和关注的例子吗?

PS:我已经添加了推特框架,并且导入了

解决方案

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

首先,添加 AccountsTwitter 框架.

现在,让我们在用户的设备上显示 Twitter 帐户.

#import <Accounts/Accounts.h>-(无效)getTwitterAccounts {ACAccountStore *accountStore = [[ACAccountStore alloc] init];//创建确保检索 Twitter 帐户的帐户类型.ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];//让我们请求访问并获取帐户[accountStore 请求AccessToAccountsWithType:accountTypewithCompletionHandler:^(BOOL 已授予,NSError *error) {//检查用户是否授予我们访问权限并且没有错误(例如没有在用户设备上添加帐户)如果(授予 && !错误){NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];if ([accountsArray count] > 1) {//用户可能有一个或多个帐户添加到他们的设备//您需要显示提示或单独的视图,让用户选择您需要获取关注者和朋友的帐户} 别的 {[self getTwitterFriendsForAccount:[accountsArray objectAtIndex:0]];}} 别的 {//处理错误(显示用户未授予您的应用访问权限等信息的警报)}}];}

现在我们可以使用 GET Friends/ids 命令:

#import <Twitter/Twitter.h>-(void)getTwitterFriendsForAccount:(ACAccount*)account {//在这种情况下,我正在为帐户创建一个字典//添加账户网名NSMutableDictionary *accountDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:account.username, @"screen_name", nil];//添加用户 ID(在我的情况下我需要它,但执行请求不是必需的)[accountDictionary setObject:[[[account dictionaryWithValuesForKeys:[NSArray arrayWithObject:@"properties"]] objectForKey:@"properties"] objectForKey:@"user_id"] forKey:@"user_id"];//设置 URL,你可以看到它只是 Twitter 自己的 API url 方案.在这种情况下,我们希望以 JSON 格式接收它NSURL *followingURL = [NSURL URLWithString:@"http://api.twitter.com/1/friends/ids.json"];//传入参数(基本上是'.ids.json?screen_name=[screen_name]')NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:account.username, @"screen_name", nil];//设置请求TWRequest *twitterRequest = [[TWRequest alloc] initWithURL:followingURL参数:参数requestMethod:TWRequestMethodGET];//这个很重要!为请求设置帐户,以便我们可以执行经过身份验证的请求.如果没有这个,您将无法获得私人帐户的关注者,如果您执行的请求过多,Twitter 也可能会返回错误[twitterRequest setAccount:account];//执行 Twitter 好友请求[twitterRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {如果(错误){//处理任何错误 - 请记住,尽管您可能会收到包含错误的有效响应,因此您可能需要查看响应并确保字典中不存在 'error:' 键}NSError *jsonError = nil;//将响应转换为字典NSDictionary *twitterFriends = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONWritingPrettyPrinted error:&jsonError];//获取 Twitter 返回的 Id 并将它们添加到我们之前创建的字典中[accountDictionary setObject:[twitterFriends objectForKey:@"ids"] forKey:@"friends_ids"];NSLog(@"%@", accountDictionary);}];}

当您想要一个帐户的关注者时,几乎相同...只需使用 URL http://api.twitter.com/1/followers/ids.format 并传入通过 GET follower/ids

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

更新:

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

I want to be able to use the Twitter API for ios 5 to get all of the user followers and following user name into a NSDictionary...

I've hit a road block though. I don't know how to use the Twitter API the do this... But my main problem is getting the user's username in the first place. How can I make an API request to find this users followers when I don't even know the users username?

Can someone give me an example on getting your Twitter users followers and following?

PS: I've already added the Twitter framework, and imported

解决方案

It's a combination of Apple's Twitter API and Twitter's own API. It's fairly straight forward once you read the code. I'm going to provide sample code for how to get the 'friends' for a Twitter account (this is the term for people that a user follows), which should be enough to get you going on a method to obtain the followers for an account.

First, add the Accounts and Twitter frameworks.

Now, let's get the Twitter account(s) present on a user's device.

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

Now we can get the friends for an account using the GET friends/ids command:

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

When you want the followers for an account, it's almost the same... Simple use the URL http://api.twitter.com/1/followers/ids.format and pass in the needed parameters as found via GET followers/ids

Hope this gives you a good head start.

UPDATE:

As pointed out in the comments, you should be using the updated API call: https://api.twitter.com/1.1/followers/list.json

这篇关于iPhone- Twitter API 获取用户关注者/关注者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何在 iOS 5 故事板中初始化自定义原型样式表单元格? 下一篇:Core Image CIColorControls 亮度滤镜会产生错误的效果.如何更改图像的亮度?

相关文章