如何使用twitter api v1.1获取用户详细信息(Twitter错误215) [英] How to get user details using twitter api v1.1 (Twitter error 215)
问题描述
我使用了twitter提供的twitter api来获取详细信息,但
无法执行它,甚至尝试传递身份验证数据
,如消费者密钥,消费者密钥,令牌但是结果是一样的。
我能够登录并接收Twitter身份验证令牌,但无法获取用户详细信息。
下面的代码由我使用(我正在使用MGtwitter引擎):
I have used the twitter api provided by twitter,to get the details but
not able to execute it, even tried to pass the authentication data
like consumer secret key, consumer key, token but the result is same.
I am able to login and receiving twitter authentication token but not able to get user details.
Below code is used by me (I am using MGtwitter engine) :
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";
} );
推荐答案
首先,您需要验证
您的请求(获取权限)。
First, you need to Authenticate
your request (Get permission).
秒,请参阅这些步骤:
1.下载 FHSTwitterEngine Twitter图书馆。
1.Download FHSTwitterEngine Twitter Library.
2.将文件夹 FHSTwitterEngine
添加到您的项目中, #importFHSTwitterEngine.h
。
2.Add the folder FHSTwitterEngine
" to your project and #import "FHSTwitterEngine.h"
.
3.add SystemConfiguration.framework
到你的项目。
3.add SystemConfiguration.framework
to your project.
用法: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
。
- 您需要获得许可对于您的请求,使用以下方法显示登录窗口:
- (void)showLoginWindow:(id)sender {
[[FHSTwitterEngine sharedEngine]showOAuthLoginControllerFromViewController:self withCompletion:^(BOOL success) {
NSLog(success?@"L0L success":@"O noes!!! Loggen faylur!!!");
}];
}
当显示登录窗口时,输入您的Twitter 用户名
和密码
到验证
您的请求。
when the Login window is presented, enter your Twitter Username
and Password
to authenticate
your request.
- 将以下方法添加到您的代码中:
-(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.现在你准备好了请求,使用以下方法(在此方法中我创建了
Hashtag
,以获取screen_name
例如):
4.Now you are ready to get your request, with the following method(in this method I created a
Hashtag
, to get thescreen_name
for example):
- (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;
}
});
}
});
}
这就是全部。
我刚从搜索查询中获取了screen_name,您可以使用以下方法获取用户的时间表:
That's all. I just got the screen_name from a search Query, you can get a timeline for a user using the following methods:
// 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;
而不是上面的搜索方法。
instead of the search method above.
注意:请参阅 FHSTwitterEngine.h
以了解您需要使用的方法。
注意:要获得< consumer_key>
和< consumer_secret>
,您需要访问这个链接
在Twitter上注册您的应用。
Note: see the FHSTwitterEngine.h
to know what method you need to use.
Note: to get the <consumer_key>
and the <consumer_secret>
you need to to visit this link
to register your app in Twitter.
这篇关于如何使用twitter api v1.1获取用户详细信息(Twitter错误215)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!