0

我在我的应用程序中使用 MSTwitterEngine,将 twitter api 更新到 v1.1,我无法获取用户信息、关注者列表和发送直接消息。

我试图调用新的 api url,但得到错误的身份验证作为响应。

请看下面的片段:-

参考:- https://dev.twitter.com/docs/api/1.1/get/followers/list

 NSString *strUrl = [NSString stringWithFormat:@"https://api.twitter.com/1.1/followers/list.json?cursor=-1&screen_name=%@",strUserName];


NSURL *url=[NSURL URLWithString:[strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *mRequest = [NSMutableURLRequest requestWithURL:url];

[mRequest setValue:@"application/json" forHTTPHeaderField:@"content-type"];
[mRequest setHTTPMethod:@"POST"];

NSURLResponse *response = nil;
NSError *error = nil;
NSData *responseData1  = [NSURLConnection sendSynchronousRequest:mRequest returningResponse:&response error:&error];
if (error!=nil) {


}
NSString *responseString = [[NSString alloc]initWithData:responseData1 encoding:NSUTF8StringEncoding];
SBJSON *json=[SBJSON new];
NSDictionary *responseDic=[json objectWithString:responseString error:&error];

响应:- {"errors":[{"message":"Bad Authentication data","code":215}]}

4

2 回答 2

0

您上面的代码很好,但唯一的事情是设置所选的推特帐户,所以这样做,

NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/followers/ids.json"];
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setObject:your_user_id forKey:@"user_id"];
[parameters setObject:@"-1" forKey:@"cursor"];

TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:parameters requestMethod:TWRequestMethodGET];
[request setAccount:your_twitter_account];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
 {
     if (responseData) {

         NSError *error = nil;

         NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
         if([dict objectForKey:@"error"]!=nil)
         {

         }
         else
         {

         }
     }
 }];
[parameters release];
[request release];

这对我有用..希望它会帮助你..

于 2013-07-02T06:33:58.290 回答
0

新的 twitter API 需要登录才能从 twitter 获取数据,正如他们在文档中提到的那样,执行任何 twitter 相关功能都需要身份验证。

所以现在要执行任何功能,您应该使用 OAuth 或 Twitter 框架登录

于 2013-07-02T06:24:01.503 回答