OGeek|极客世界-中国程序员成长平台

标题: iphone - Twitter API(从 ID 到屏幕名称) [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 15:08
标题: iphone - Twitter API(从 ID 到屏幕名称)

我正在尝试将 Twitter 用户 ID(例如:77687121)转换为他们的用户名或屏幕名称(任何一个都可以),但我遇到了问题...

我尝试按照这个 SO 问题的指示,但是对于 Twitter api 以及 Objective c 本身来说都是新手,这让我很难理解:http://stackoverflow.com/questions/10020672/php- twitter-api-get-users-lookup-grabbing-screen-name-from-id

我从该教程中编写了以下代码,我得到了这个......

- (void) turnFriendId: (NSString *)friID {
// 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
NSLog(@"%@",friID);
NSURL *followingURL = [NSURL URLWithString"https://api.twitter.com/1/users/show.json"];
// Pass in the parameters (basically '.ids.json?screen_name=[screen_name]')
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:friID, @"id_str", 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:theAccount];
// 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 *twitterGrabbedUserInfo = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONWritingPrettyPrinted error:&jsonError];
    // Grab the Ids that Twitter returned and add them to the dictionary we created earlier
    NSLog(@"%@", [twitterGrabbedUserInfo objectForKey"name"]);
}];
}

这种方法有时会给我一个错误,有时它会中断并显示一些汇编代码...下面是错误... 以及第一个 NSLOG();在方法的开头...

2012-07-23 18:42:05.786 VideoPush[17625:3503] 1882208
2012-07-23 18:42:17.692 VideoPush[17625:3503] -[__NSCFType credentialForAccount:]: unrecognized selector sent to instance 0x1afeb0
2012-07-23 18:42:17.694 VideoPush[17625:3503] -[__NSCFType enabledDataclassesForAccount:]: unrecognized selector sent to instance 0x1afeb0
2012-07-23 18:42:17.696 VideoPush[17625:3503] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType enabledDataclassesForAccount:]: unrecognized selector sent to instance 0x1afeb0'
*** First throw call stack:
(0x355e188f 0x37988259 0x355e4a9b 0x355e3915 0x3553e650 0x3055bfc3 0x3055b98b 0x35065d2f 0x355c1975 0x3553c1df 0x3554fb5f 0x35080b8b 0x35080b2b 0x3257dfdf 0x3257e88d 0x3257ea6b 0x3257eea5 0x355e3a83 0x3553e650 0x3055cf85 0x322047b1 0x32204e5d 0x31f1f 0x31d8d 0x32204f8b 0x3446ec59 0x34470d0f 0x34470b75 0x344717e7 0x3299edfb 0x3299ecd0)
terminate called throwing an exception(lldb)

这也是一个很好的方法来遍历每个单独的 Twitter id(即使我有数千个)只是为了获取每个用户的屏幕名称。我知道 Twitter 有 API 限制...



Best Answer-推荐答案


运行 users/lookup 调用

http://api.twitter.com/1/users/lookup.json?user_id=77687121,43662011,6253282

您可以用逗号分隔 I.D 以调用一个电话。

响应示例请看这里:

https://snap.apigee.com/NHcXs4

关于iphone - Twitter API(从 ID 到屏幕名称),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11621677/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4