如何在iOS中获取twitter个人资料图片URL?
问题描述:
我曾经这样做:如何在iOS中获取twitter个人资料图片URL?
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。那么,我已经认证了。那么我怎么知道?
答
这是做
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;
答
您需要设置
NSString * strPictureURL = [NSString stringWithFormat:@"https://api.twitter.com/1.1/users/profile_image?screen_name=%@&size=bigger",strUsername.RobustURLEncodedString];
,而不是第1版,使用1.1版
+1
代码https://api.twitter.com/1.1/users/profile_image?screen_name=teguh123&size=bigger不起作用。 –
答
可以使用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);
}
}];
这段代码缺少许多组件。 – AddisDev
完美运作! –