如何通过iPhone sdk中的新twitter api 1.1 url(https://api.twitter.com/1.1/)获取数据?

时间:2013-07-02 06:12:32

标签: iphone ios ios4 twitter

我在我的应用程序中使用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];

Resposne: - {“errors”:[{“message”:“Bad Authentication data”,“code”:215}]}

2 个答案:

答案 0 :(得分:0)

新的Twitter API需要登录才能从Twitter获取数据,因为他们在文档中已经提到认证是执行任何推特相关功能所必需的。

现在要做任何功能,你应该使用OAuth或Twitter框架登录

答案 1 :(得分:0)

您的上述代码很好,但只有设置所选的Twitter帐户,所以这样做,

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];

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