我正在使用MGTwitterEngine lib对Twitter帐户进行身份验证,但在成功登录后,我想获取用户个人资料图片但无法获取,并且以下方法已停止工作:
https://api.twitter.com/1.1/users/profile_image?screen_name=username&size=bigger
我刚试过上面的方法但没有成功。然后我做了很多关于它的研究,我终于得到了api来获取用户个人资料信息,其中包括个人资料照片:
https://api.twitter.com/1.1/users/show.json?screen_name=username
但是这个api需要通过在Request头中传递Authorization来进行身份验证,这是我无法生成的。
这是我尝试过的代码:
此处authdata是我成功登录后从MGTwitterEngine lib返回的字符串。
NSString *authData = [[NSUserDefaults standardUserDefaults] valueForKey:@"authData"];
NSString *subAuthdata = [authData substringFromIndex:[authData rangeOfString:@"oauth_token_secret="].location];
subAuthdata = [subAuthdata substringToIndex:[subAuthdata rangeOfString:@"&user_id="].location];
NSLog(@"%@", subAuthdata);
NSLog(@"encoded: %@", [self base64Encode:subAuthdata]);
NSString *accessTokenHeaderToPost = [NSString stringWithFormat:@"Basic %@", [self base64Encode:subAuthdata]];
NSString *twitURL = @"https://api.twitter.com/1.1/users/show.json?screen_name=username";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString: twitURL]];
[request setHTTPMethod:@"GET"];
[request setValue:accessTokenHeaderToPost forHTTPHeaderField:@"Authorization"];
NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog (@"RESP: %@", responseString);
但它总是说不好的身份验证数据。
请帮帮我...
答案 0 :(得分:10)
以下是使用STTwitter的解决方案:
STTwitterAPI *twitter = [STTwitterAPI twitterAPIAppOnlyWithConsumerKey:@"" consumerSecret:@""];
[twitter verifyCredentialsWithSuccessBlock:^(NSString *bearerToken) {
[twitter getUsersShowForUserID:nil orScreenName:@"barackobama" includeEntities:nil successBlock:^(NSDictionary *user) {
NSString *profileImageURLString = [user valueForKey:@"profile_image_url"];
NSURL *url = [NSURL URLWithString:profileImageURLString];
UIImage *profileImage = [UIImage imageWithContentsOfURL:url];
} errorBlock:^(NSError *error) {
//
}];
} errorBlock:^(NSError *error) {
//
}];