如何使用twitter api v1.1获取用户详细信息(Twitter错误215)
我已经使用twitter提供的twitter API来获取详细信息,但无法执行它,甚至试图传递消费者密钥,消费者密钥,令牌等身份validation数据,但结果是一样的。
我能够login并接收Twitter身份validation令牌,但无法获取用户详细信息。 下面的代码是我使用的(我正在使用MGtwitter引擎):
NSMutableURLRequest *request =[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.twitter.com/1.1/users/show.json?screen_name=%@",username]]]; NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ]; NSString *returnString = [[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding]; NSError *err = nil; twitterLogin = [NSJSONSerialization JSONObjectWithData:[returnString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&err];
错误如下所示:
errors = ( { code = 215; message = "Bad Authentication data"; } );
首先,您需要Authenticate
您的请求(获得许可)。
第二,请参阅以下步骤 :
1.下载FHSTwitterEngine Twitter库。
2.将文件夹FHSTwitterEngine
到您的项目和#import "FHSTwitterEngine.h"
。
3.将SystemConfiguration.framework
到您的项目。
用法:1.在
[ViewDidLoad]
添加下面的代码。
UIButton *logIn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; logIn.frame = CGRectMake(100, 100, 100, 100); [logIn setTitle:@"Login" forState:UIControlStateNormal]; [logIn addTarget:self action:@selector(showLoginWindow:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:logIn]; [[FHSTwitterEngine sharedEngine]permanentlySetConsumerKey:@"<consumer_key>" andSecret:@"<consumer_secret>"]; [[FHSTwitterEngine sharedEngine]setDelegate:self];
并且不要忘记导入委托FHSTwitterEngineAccessTokenDelegate
。
- 你需要获得你的请求的权限,用下面的方法将显示login窗口:
- (void)showLoginWindow:(id)sender { [[FHSTwitterEngine sharedEngine]showOAuthLoginControllerFromViewController:self withCompletion:^(BOOL success) { NSLog(success?@"L0L success":@"O noes!!! Loggen faylur!!!"); }]; }
当login窗口出现时,input您的Twitter Username
和Password
来authenticate
您的请求。
- 将以下方法添加到您的代码中:
-(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[FHSTwitterEngine sharedEngine]loadAccessToken]; NSString *username = [[FHSTwitterEngine sharedEngine]loggedInUsername];// self.engine.loggedInUsername; if (username.length > 0) { lbl.text = [NSString stringWithFormat:@"Logged in as %@",username]; [self listResults]; } else { lbl.text = @"You are not logged in."; } } - (void)storeAccessToken:(NSString *)accessToken { [[NSUserDefaults standardUserDefaults]setObject:accessToken forKey:@"SavedAccessHTTPBody"]; } - (NSString *)loadAccessToken { return [[NSUserDefaults standardUserDefaults]objectForKey:@"SavedAccessHTTPBody"]; }
4.现在你已经准备好用你的请求了,用下面的方法(在这个方法中,我创build了一个
Hashtag
的screen_name
):
- (void)listResults { dispatch_async(GCDBackgroundThread, ^{ @autoreleasepool { [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; // the following line contains a FHSTwitterEngine method wich do the search. dict = [[FHSTwitterEngine sharedEngine]searchTweetsWithQuery:@"#iOS" count:100 resultType:FHSTwitterEngineResultTypeRecent unil:nil sinceID:nil maxID:nil]; // NSLog(@"%@",dict); NSArray *results = [dict objectForKey:@"statuses"]; // NSLog(@"array text = %@",results); for (NSDictionary *item in results) { NSLog(@"text == %@",[item objectForKey:@"text"]); NSLog(@"name == %@",[[item objectForKey:@"user"]objectForKey:@"name"]); NSLog(@"screen name == %@",[[item objectForKey:@"user"]objectForKey:@"screen_name"]); NSLog(@"pic == %@",[[item objectForKey:@"user"]objectForKey:@"profile_image_url_https"]); } dispatch_sync(GCDMainThread, ^{ @autoreleasepool { UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Complete!" message:@"Your list of followers has been fetched" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [av show]; [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; } }); } }); }
就这样。 我刚刚从search查询中获得了screen_name,可以使用以下方法获取用户的时间轴:
// statuses/user_timeline - (id)getTimelineForUser:(NSString *)user isID:(BOOL)isID count:(int)count; - (id)getTimelineForUser:(NSString *)user isID:(BOOL)isID count:(int)count sinceID:(NSString *)sinceID maxID:(NSString *)maxID;
而不是上面的search方法。
注意:请参阅FHSTwitterEngine.h
以了解您需要使用哪种方法。 注意:要获取<consumer_key>
和<consumer_secret>
您需要访问此链接以在Twitter中注册您的应用程序。
MKAlatrash回复后得到的解决scheme,以获得用户configuration文件遵循代码中的某些步骤,如下所示:
[[FHSTwitterEngine sharedEngine]getProfileImageForUsername:username andSize:FHSTwitterEngineImageSizeNormal];
跳转到这个函数的定义并replaceif … else if部分
if ([userShowReturn isKindOfClass:[NSError class]]) { return [NSError errorWithDomain:[(NSError *)userShowReturn domain] code:[(NSError *)userShowReturn code] userInfo:[NSDictionary dictionaryWithObject:request forKey:@"request"]]; NSLog(@"user show return %@",userShowReturn); } else if ([userShowReturn isKindOfClass:[NSDictionary class]]) { return userShowReturn; NSString *url = [userShowReturn objectForKey:@"profile_image_url"]; // normal if (size == 0) { // mini url = [url stringByReplacingOccurrencesOfString:@"_normal" withString:@"_mini"]; } else if (size == 2) { // bigger url = [url stringByReplacingOccurrencesOfString:@"_normal" withString:@"_bigger"]; } else if (size == 3) { // original url = [url stringByReplacingOccurrencesOfString:@"_normal" withString:@""]; } id ret = [self sendRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]]; if ([ret isKindOfClass:[NSData class]]) { return [UIImage imageWithData:(NSData *)ret]; } return ret; }
这真的很有帮助谢谢