iOS-如何获取Twitter帐户访问权限?

3

我正在尝试使用Twitter API并获取响应。

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

我正在使用来自Twitter网站的代码。我已经在模拟器中设置了Twitter帐户,并在设备上尝试过(也尝试了多次通过注销和重新登录),但我无法获得授权访问

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

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

那么,我需要做什么额外的工作?欢迎提供任何评论或回答。
2个回答

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

现在这个 Twitter API 已经过时了。 - Tirth

-1
我建议使用Parse.com来更好、更容易地实现Twitter和Facebook的集成。你只需要几行代码就可以同时拥有它们,而Parse会处理其余部分。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接