iPhone- Twitter API GET用户关注/关注 [英] iPhone- Twitter API GET Users Followers/Following

查看:109
本文介绍了iPhone- Twitter API GET用户关注/关注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用Twitter API for ios 5将所有用户关注者和关注用户名转换为 NSDictionary ...

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...

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

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?

有人能给我一个关于获取Twitter用户关注者的示例以下?

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

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

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

推荐答案

这是Apple的Twitter API和Twitter自己的API的组合。一旦你阅读了代码,这是相当直接的。我将提供如何获取Twitter帐户的朋友的示例代码(这是用户关注的人的术语),这应该足以让您使用方法获取关注者帐号。

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.

首先,添加帐户 Twitter 框架。

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

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

现在我们可以使用获取朋友/ ids 命令:

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

当你想要一个帐户的粉丝时,它几乎是一样的......只需使用URL http://api.twitter.com/1/followers/ids.format ,并通过获取关注者/ ids

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.

更新:

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

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 GET用户关注/关注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆