gpt4 book ai didi

iPhone- Twitter API GET Users Followers/Following 用户

转载 作者:可可西里 更新时间:2023-11-01 03:02:07 26 4
gpt4 key购买 nike

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

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

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

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

最佳答案

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

首先,添加AccountsTwitter 框架。

现在,让我们获取用户设备上的 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.)
}
}];
}

现在我们可以使用 GET friends/ids 获取 friend 的帐户命令:

#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 并传入通过 GET followers/ids 找到的所需参数

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

更新:

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

关于iPhone- Twitter API GET Users Followers/Following 用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11600621/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com