获取推特 public 供稿为 JSON
Fetching twitter public feeds as JSON
作为我项目的一部分,我需要在 iOS 应用程序中显示推文列表。我尝试使用以下代码,但它返回 JSON。相同的代码在版本 1 上运行良好。现在推特 api 版本是 1.1,我收到另一个警告 TWRequest is deprecated
。即使我对 SLRequest
也有同样的看法,但这种弃用并不是问题所在。我的问题是我需要在没有身份验证或身份验证的情况下获取特定用户推文的 JSON
TWRequest *postequest=[[TWRequest alloc]initWithURL:[NSURL URLWithString:@"https://api.twitter.com/1.0/statuses/user_timeline.json?screen_name=coolCracker&count=2"] parameters:nil requestMethod:TWRequestMethodGET];
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output;
if ([urlResponse statusCode]==200) {
NSError *err;
NSDictionary *PublicTimeline=[NSJSONSerialization JSONObjectWithData:responseData options:0 error:&err];
output=[NSString stringWithFormat:@"HTTP response status: %li\nPublic timeline:\n%@",(long)[urlResponse statusCode],PublicTimeline];
}
else
{
output=[NSString stringWithFormat:@"No feed HTTP response was: %li\n",(long)[urlResponse statusCode]];
}
[self performSelectorOnMainThread:@selector(displayResults:) withObject:output waitUntilDone:NO];
}];
-(void)displayResults:(NSString *)text
{
NSLog(@"tweets feed %@",text);
}
嗨,下面的代码对我有用。虽然 TWRequest 已被弃用,但它现在可以使用。稍后您可以将其更改为 SLRequest。
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *twitterType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
// Request access from the user to use their Twitter accounts.
[accountStore requestAccessToAccountsWithType:twitterType options:nil completion:^(BOOL granted, NSError *error) {
if (granted == YES){
//get accounts to use.
NSArray *accounts = [accountStore accountsWithAccountType:twitterType];
if ([accounts count] > 0) {
NSLog(@"account: %@", accounts);
ACAccount *loggedinaccount;
loggedinaccount = accounts[0];
// get tweets
TWRequest *postRequest=[[TWRequest alloc]initWithURL:[NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=HuntEthane&count=2"] parameters:nil requestMethod:TWRequestMethodGET];
[postRequest setAccount:loggedinaccount];
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output;
if ([urlResponse statusCode]==200) {
NSError *err;
NSDictionary *PublicTimeline=[NSJSONSerialization JSONObjectWithData:responseData options:0 error:&err];
output=[NSString stringWithFormat:@"HTTP response status: %li\nPublic timeline:\n%@",(long)[urlResponse statusCode],PublicTimeline];
}
else
{
output=[NSString stringWithFormat:@"No feed HTTP response was: %li\n",(long)[urlResponse statusCode]];
}
[self performSelectorOnMainThread:@selector(displayResults:) withObject:output waitUntilDone:NO];
}];
}
}else{
// show alert to inser user_name and password to login on twitter
UIAlertView *alertTwitterAccount = [[UIAlertView alloc]
initWithTitle:@"Enter with your twitter account"
message:nil
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Login", nil];
[alertTwitterAccount setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
[[alertTwitterAccount textFieldAtIndex:0] setPlaceholder:@"User"];
[alertTwitterAccount setDelegate:self];
[alertTwitterAccount setTag:1];
[alertTwitterAccount show];
}
}];
记得添加以下框架以使此代码正常工作。
- Twitter.FrameWork
- Social.FrameWork
- Accounts.Framework
Security.FrameWork
如果您想使用 SLRequest
而不是 TWRequest
,请将请求语句替换为以下语句
SLRequest *postRequest = [SLRequest
requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodGET
URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=HuntEthane&count=2"]parameters:nil];
前进最简单的方法是采用来自 Tweeter 的 Fabric.io,因为开发平台包括 Tweeter 嵌入 :https://get.fabric.io/native-social
"The easiest way to bring Twitter content into your app."
老实说,我没有研究 Fabric.io 所需的身份验证部分。
否则您需要遵守并遵守访问 Twitter REST APIs 或 Streaming APIs[=36= 的规则],两者都有据可查。 OAuth 需要身份验证。
如果您希望避免在您的应用程序中进行身份验证,您可以编写一个简单的 Web REST API 来处理身份验证并向您的 iOS 应用程序提供(选定的)推文。
- 首先,您需要在 Twitter 上注册一个应用程序
- 写下您的 Consumer Key 和 Consumer Secret
- 用您选择的语言编写一个简单的服务器 API(代理)。
I personally prefer JavaScript on Node.js. Consider using libraries like express.js and oauth available on npm, but you can search npm for any other implemented Twitter proxy/API.
The code essentially consists of getting an OAuth Access token and then calling selected twitter APIs, and respecting ratelimits by caching results.
我在工作中使用了它并且效果很好。使用 STT 推特。下载 STTwitter 文件 here.
将此放在您的视图中 didload:
STTwitterAPI *twitter = [STTwitterAPI twitterAPIAppOnlyWithConsumerKey:@"your consumer key"
consumerSecret:@"your secret key"];
[twitter verifyCredentialsWithSuccessBlock:^(NSString *bearerToken) {
[twitter getUserTimelineWithScreenName:@"your twitter account name"
successBlock:^(NSArray *statuses) {
self.twitterFeedList = [NSMutableArray arrayWithArray:statuses];
[self->listView reloadData];
} errorBlock:^(NSError *error) {
NSLog(@"%@", error.debugDescription);
}];
} errorBlock:^(NSError *error) {
NSLog(@"%@", error.debugDescription);
}];
作为我项目的一部分,我需要在 iOS 应用程序中显示推文列表。我尝试使用以下代码,但它返回 JSON。相同的代码在版本 1 上运行良好。现在推特 api 版本是 1.1,我收到另一个警告 TWRequest is deprecated
。即使我对 SLRequest
也有同样的看法,但这种弃用并不是问题所在。我的问题是我需要在没有身份验证或身份验证的情况下获取特定用户推文的 JSON
TWRequest *postequest=[[TWRequest alloc]initWithURL:[NSURL URLWithString:@"https://api.twitter.com/1.0/statuses/user_timeline.json?screen_name=coolCracker&count=2"] parameters:nil requestMethod:TWRequestMethodGET];
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output;
if ([urlResponse statusCode]==200) {
NSError *err;
NSDictionary *PublicTimeline=[NSJSONSerialization JSONObjectWithData:responseData options:0 error:&err];
output=[NSString stringWithFormat:@"HTTP response status: %li\nPublic timeline:\n%@",(long)[urlResponse statusCode],PublicTimeline];
}
else
{
output=[NSString stringWithFormat:@"No feed HTTP response was: %li\n",(long)[urlResponse statusCode]];
}
[self performSelectorOnMainThread:@selector(displayResults:) withObject:output waitUntilDone:NO];
}];
-(void)displayResults:(NSString *)text
{
NSLog(@"tweets feed %@",text);
}
嗨,下面的代码对我有用。虽然 TWRequest 已被弃用,但它现在可以使用。稍后您可以将其更改为 SLRequest。
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *twitterType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
// Request access from the user to use their Twitter accounts.
[accountStore requestAccessToAccountsWithType:twitterType options:nil completion:^(BOOL granted, NSError *error) {
if (granted == YES){
//get accounts to use.
NSArray *accounts = [accountStore accountsWithAccountType:twitterType];
if ([accounts count] > 0) {
NSLog(@"account: %@", accounts);
ACAccount *loggedinaccount;
loggedinaccount = accounts[0];
// get tweets
TWRequest *postRequest=[[TWRequest alloc]initWithURL:[NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=HuntEthane&count=2"] parameters:nil requestMethod:TWRequestMethodGET];
[postRequest setAccount:loggedinaccount];
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output;
if ([urlResponse statusCode]==200) {
NSError *err;
NSDictionary *PublicTimeline=[NSJSONSerialization JSONObjectWithData:responseData options:0 error:&err];
output=[NSString stringWithFormat:@"HTTP response status: %li\nPublic timeline:\n%@",(long)[urlResponse statusCode],PublicTimeline];
}
else
{
output=[NSString stringWithFormat:@"No feed HTTP response was: %li\n",(long)[urlResponse statusCode]];
}
[self performSelectorOnMainThread:@selector(displayResults:) withObject:output waitUntilDone:NO];
}];
}
}else{
// show alert to inser user_name and password to login on twitter
UIAlertView *alertTwitterAccount = [[UIAlertView alloc]
initWithTitle:@"Enter with your twitter account"
message:nil
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Login", nil];
[alertTwitterAccount setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
[[alertTwitterAccount textFieldAtIndex:0] setPlaceholder:@"User"];
[alertTwitterAccount setDelegate:self];
[alertTwitterAccount setTag:1];
[alertTwitterAccount show];
}
}];
记得添加以下框架以使此代码正常工作。
- Twitter.FrameWork
- Social.FrameWork
- Accounts.Framework
Security.FrameWork
如果您想使用
SLRequest
而不是TWRequest
,请将请求语句替换为以下语句SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=HuntEthane&count=2"]parameters:nil];
前进最简单的方法是采用来自 Tweeter 的 Fabric.io,因为开发平台包括 Tweeter 嵌入 :https://get.fabric.io/native-social
"The easiest way to bring Twitter content into your app."
老实说,我没有研究 Fabric.io 所需的身份验证部分。
否则您需要遵守并遵守访问 Twitter REST APIs 或 Streaming APIs[=36= 的规则],两者都有据可查。 OAuth 需要身份验证。
如果您希望避免在您的应用程序中进行身份验证,您可以编写一个简单的 Web REST API 来处理身份验证并向您的 iOS 应用程序提供(选定的)推文。
- 首先,您需要在 Twitter 上注册一个应用程序
- 写下您的 Consumer Key 和 Consumer Secret
- 用您选择的语言编写一个简单的服务器 API(代理)。
I personally prefer JavaScript on Node.js. Consider using libraries like express.js and oauth available on npm, but you can search npm for any other implemented Twitter proxy/API. The code essentially consists of getting an OAuth Access token and then calling selected twitter APIs, and respecting ratelimits by caching results.
我在工作中使用了它并且效果很好。使用 STT 推特。下载 STTwitter 文件 here.
将此放在您的视图中 didload:
STTwitterAPI *twitter = [STTwitterAPI twitterAPIAppOnlyWithConsumerKey:@"your consumer key"
consumerSecret:@"your secret key"];
[twitter verifyCredentialsWithSuccessBlock:^(NSString *bearerToken) {
[twitter getUserTimelineWithScreenName:@"your twitter account name"
successBlock:^(NSArray *statuses) {
self.twitterFeedList = [NSMutableArray arrayWithArray:statuses];
[self->listView reloadData];
} errorBlock:^(NSError *error) {
NSLog(@"%@", error.debugDescription);
}];
} errorBlock:^(NSError *error) {
NSLog(@"%@", error.debugDescription);
}];