xcode 如何使用 twitter api v1.1 获取用户详细信息(Twitter 错误 215)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17081012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get user details using twitter api v1.1 (Twitter error 215)
提问by Raviraj Jadeja
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) :
我使用了 twitter 提供的 twitter api 来获取详细信息但无法执行它,甚至尝试传递诸如消费者密钥、消费者密钥、令牌之类的身份验证数据,但结果是一样的。
我可以登录并接收 Twitter 身份验证令牌,但无法获取用户详细信息。以下代码由我使用(我使用的是 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];
Error is shown as below:
错误显示如下:
errors = (
{
code = 215;
message = "Bad Authentication data";
} );
回答by M.Alatrash
First, you need to Authenticate
your request (Get permission).
首先,你需要Authenticate
你的请求(获得许可)。
second, see follow these steps:
其次,请参阅以下步骤:
1.Download FHSTwitterEngineTwitter Library.
1.下载FHSTwitterEngineTwitter 库。
2.Add the folder FHSTwitterEngine
" to your project and #import "FHSTwitterEngine.h"
.
2.将文件夹FHSTwitterEngine
“添加到您的项目和#import "FHSTwitterEngine.h"
.
3.add SystemConfiguration.framework
to your project.
3.添加SystemConfiguration.framework
到您的项目中。
Usage : 1.in the
[ViewDidLoad]
add the following code.
用法: 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];
and don't forgetto import the delegate FHSTwitterEngineAccessTokenDelegate
.
并且不要忘记导入委托FHSTwitterEngineAccessTokenDelegate
。
- you need to get the permission for your request, with the following method which will present Login window:
- 您需要使用以下方法获得您的请求的许可,该方法将显示登录窗口:
- (void)showLoginWindow:(id)sender {
[[FHSTwitterEngine sharedEngine]showOAuthLoginControllerFromViewController:self withCompletion:^(BOOL success) {
NSLog(success?@"L0L success":@"O noes!!! Loggen faylur!!!");
}];
}
when the Login window is presented, enter your Twitter Username
and Password
to authenticate
your request.
呈现在登录窗口时,输入你的TwitterUsername
和Password
对authenticate
你的要求。
- add the following methods to your code:
- 将以下方法添加到您的代码中:
-(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.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):
4.现在您已准备好使用以下方法获取您的请求(在此方法中,我创建了
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;
}
});
}
});
}
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:
就这样。我刚刚从搜索查询中获得了 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;
instead of the search method above.
而不是上面的搜索方法。
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 linkto register your app in Twitter.
注意:请参阅FHSTwitterEngine.h
了解您需要使用的方法。注意:要获取<consumer_key>
和 ,<consumer_secret>
您需要访问此链接以在 Twitter 中注册您的应用程序。
回答by Raviraj Jadeja
Got the solution after MKAlatrash revert, to get the user profile follow certain steps in the code as under :
在 MKalatrash 恢复后得到解决方案,按照代码中的某些步骤获取用户配置文件,如下所示:
[[FHSTwitterEngine sharedEngine]getProfileImageForUsername:username andSize:FHSTwitterEngineImageSizeNormal];
jump to definition of this function and replace the if ... else if part
跳转到此函数的定义并替换 if ... 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;
}
That really was helpful thanks
这真的很有帮助谢谢