問題描述
我希望能夠使用 iOS 5 的 Twitter API 來獲取所有用戶關注者和關注用戶名到 NSDictionary
...
不過,我遇到了障礙.我不知道如何使用 Twitter API 來做到這一點......但我的主要問題是首先獲取用戶的用戶名.當我什至不知道用戶的用戶名時,如何發出 API 請求來查找該用戶的關注者?
誰能給我一個關于讓你的 Twitter 用戶關注和關注的例子嗎?
PS:我已經添加了推特框架,并且導入了
它是 Apple 的 Twitter API 和 Twitter 自己的 API 的組合.一旦你閱讀了代碼,它就相當簡單了.我將提供示例代碼,說明如何獲取 Twitter 帳戶的朋友"(這是用戶關注的人的術語),這應該足以讓您繼續了解獲取關注者的方法帳戶.
首先,添加 Accounts
和 Twitter
框架.
現在,讓我們在用戶的設備上顯示 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 獲取用戶關注者/關注者的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!