【发布时间】:2015-11-25 21:50:43
【问题描述】:
我需要获取公共帐户的 Twitter 提要。我可以使用以下代码获取自己的 Twitter 提要:
-(void) testTwitter {
{
ACAccountStore *accountStore = [[ACAccountStore alloc]init];
if (accountStore != nil)
{
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
if (accountType != nil)
{
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error)
{
if (granted)
{
//Succesful Access
NSArray *twitterAccounts = [accountStore accountsWithAccountType:accountType];
if (twitterAccounts != nil)
{
ACAccount *currentAccount = [twitterAccounts objectAtIndex:0];
if (currentAccount != nil)
{
NSString *paraString = [NSString stringWithFormat:@"qlx_corp"];
NSDictionary *parameters = [NSDictionary dictionaryWithObject:paraString forKey:@"username"];
NSString *requestString = @"https://api.twitter.com/1.1/statuses/user_timeline.json";
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:[NSURL URLWithString:requestString] parameters:parameters];
[request setAccount:currentAccount];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
if ((error == nil) && ([urlResponse statusCode] == 200))
{
NSArray *twitterFeed = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
NSDictionary *firstPost = [twitterFeed objectAtIndex:0];
NSLog(@"firstPost = %@", [firstPost description]);
}
else {
NSLog(@"error: %@",error);
}
}];
}
}
}
else
{
//Access Denied
NSLog(@"access denied");
}
}];
}
}
}
}
但是当我用下面的代码代替我自己的另一个帐户时,我得到了这个错误:
代码 = 215;
message = "错误的身份验证数据。";
这是我替换成功访问的代码(在已发布代码的 cmets 中注明):
NSURL* url = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/user_timeline.json"];
NSDictionary* params = @{@"count" : @"10", @"screen_name" : @"qlx_corp"};
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodGET
URL:url parameters:params];
[request performRequestWithHandler:^(NSData *responseData,
NSHTTPURLResponse *urlResponse, NSError *error) {
if (!error) {
NSError *jsonError;
NSArray *responseJSON = [NSJSONSerialization
JSONObjectWithData:responseData
options:NSJSONReadingAllowFragments
error:&jsonError];
NSLog(@"response json: %@",responseJSON);
// Show the content of the responseJSON in a view.
}
}];
我做错了什么,我收到此错误消息,而不是我请求的公共 Twitter 提要。我的应用请求使用 Twitter 的权限,并且已获得批准,那么可能是什么问题?
【问题讨论】:
-
好吧,我没有使用 Twitter API,我使用的是 Apple 的社交框架。所以我试图避免使用不同的技术,但如果我找不到解决方案,那将是寻找替代方案的好地方。谢谢!
标签: ios objective-c xcode twitter