2013-08-20 47 views
3

我想使用Twitter的API并得到响应iOS-如何获取Twitter帐户访问权限?

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

我使用的代码twitter site 我已经设置Twitter帐户在模拟器也尝试在设备上(也做注销并重新记录重试很多次-in),但我无法得到访问授予

[self.accountStore 
    requestAccessToAccountsWithType:twitterAccountType 
    options:NULL 
    completion:^(BOOL granted, NSError *error) { 

      if (granted) { 
      // getting granted false i.e. I am not reaching here 
      } 
     } 

所以,我需要另外做什么?任何评论,答案是赞赏。

回答

4

要回答自己的问题,可能有助于其他。

虽然我使用的是来自twitter网站的示例https://dev.twitter.com/docs/ios/making-api-requests-slrequest它工作正常,除了ACAccountStore对象分配。

我只是在需要的地方重新分配ACAccountStore

- (void)fetchTimelineForUser:(NSString *)username 
{ 
    ACAccountStore *accountStore=[[ACAccountStore alloc]init]; 
    // Step 0: Check that the user has local Twitter accounts 
    if ([self userHasAccessToTwitter]) { 

    // Step 1: Obtain access to the user's Twitter accounts 
    ACAccountType *twitterAccountType = [accountStore 
             accountTypeWithAccountTypeIdentifier: 
             ACAccountTypeIdentifierTwitter]; 
    [accountStore 
    requestAccessToAccountsWithType:twitterAccountType 
    options:NULL 
    completion:^(BOOL granted, NSError *error) { 
     if (granted) { 
     // Step 2: Create a request 
     NSArray *twitterAccounts = 
     [accountStore accountsWithAccountType:twitterAccountType]; 
     NSURL *url = [NSURL URLWithString:@"https://api.twitter.com" 
         @"/1.1/statuses/user_timeline.json"]; 
     NSDictionary *params = @{@"screen_name" : username, 
            @"include_rts" : @"0", 
            @"trim_user" : @"1", 
            @"count" : @"1"}; 
     SLRequest *request = 
     [SLRequest requestForServiceType:SLServiceTypeTwitter 
          requestMethod:SLRequestMethodGET 
             URL:url 
           parameters:params]; 

     // Attach an account to the request 
     [request setAccount:[twitterAccounts lastObject]]; 

     // Step 3: Execute the request 
     [request performRequestWithHandler:^(NSData *responseData, 
               NSHTTPURLResponse *urlResponse, 
               NSError *error) { 
      if (responseData) { 
      if (urlResponse.statusCode >= 200 && urlResponse.statusCode < 300) { 
       NSError *jsonError; 
       NSDictionary *timelineData = 
       [NSJSONSerialization 
       JSONObjectWithData:responseData 
       options:NSJSONReadingAllowFragments error:&jsonError]; 

       if (timelineData) { 
       NSLog(@"Timeline Response: %@\n", timelineData); 
       } 
       else { 
       // Our JSON deserialization went awry 
       NSLog(@"JSON Error: %@", [jsonError localizedDescription]); 
       } 
      } 
      else { 
       // The server did not respond successfully... were we rate-limited? 
       NSLog(@"The response status code is %d", urlResponse.statusCode); 
      } 
      } 
     }]; 
     } 
     else { 
     // Access was not granted, or an error occurred 
     NSLog(@"%@", [error localizedDescription]); 
     } 
    }]; 
    } 
} 
+0

现在这个twitter API是过时的。 – Tirth

-1

我建议Parse.com更好更容易地实现Twitter和Facebook的整合。你可以用几行代码让它们都有,剩下的就是Parse。

相关问题