如何在iOS中获取twitter个人资料图片网址?

时间:2013-06-28 08:48:43

标签: objective-c twitter xcode4.5

我曾经这样做过:

NSString * strPictureURL = [NSString stringWithFormat:@"https://api.twitter.com/1/users/profile_image?screen_name=%@&size=bigger",strUsername.RobustURLEncodedString];

https://api.twitter.com/1/users/profile_image?screen_name=username&size=bigger

不再适用了。 Twitter关闭了那个API。好吧,我已经通过身份验证了。那我怎么知道?

3 个答案:

答案 0 :(得分:3)

您可以使用Fabric Twitter library

Configuration非常简单:

[[Twitter sharedInstance] startWithConsumerKey:@"_key_" consumerSecret:@"_secret_"];
[Fabric with:@[[Twitter sharedInstance]]];

使用后:

    [[Twitter sharedInstance] logInWithCompletion:^(TWTRSession *session, NSError *error) {
    if (session) {
        NSString *userID = [Twitter sharedInstance].sessionStore.session.userID;
        TWTRAPIClient *client = [[TWTRAPIClient alloc] initWithUserID:userID];
        [client loadUserWithID:userID completion:^(TWTRUser *user, NSError *error) {
            NSLog(@"Profile image url = %@", user.profileImageLargeURL);
        }];
    } else {
        NSLog(@"error: %@", error.localizedDescription);
    }
}];

答案 1 :(得分:2)

这是新的做法

            NSDictionary * resp = [NSJSONSerialization JSONObjectWithData:responseData
                                                      options:0
                                                        error:&jsonError];


            self.strUsername  = [resp objectForKey:@"screen_name"];
            NSString * strPictureURL = [resp objectForKey:@"profile_image_url"];
            strPictureURL = [strPictureURL stringByReplacingOccurrencesOfString:@"_normal" withString:@""];
            self.strPictureURL = strPictureURL;

答案 2 :(得分:1)

您需要设置

NSString * strPictureURL = [NSString stringWithFormat:@"https://api.twitter.com/1.1/users/profile_image?screen_name=%@&size=bigger",strUsername.RobustURLEncodedString];

而不是版本1,使用版本1.1