iphone - iOS-如何获取推特账号权限?

标签 iphone ios twitter

我正在尝试使用 twitter api 并获得响应

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

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

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

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

那么,我还需要做什么?欢迎任何评论和回答。

最佳答案

将回答自己的问题,可能对其他人有帮助。

尽管我使用的示例来自推特网站 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]);
       }
     }];
  }
}

关于iphone - iOS-如何获取推特账号权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18334731/

相关文章:

ios - firefox ios 删除弹出动画或修复光标未对齐

ios - 如何计算每秒的点击次数

ios - 通过 iOS App 在 Twitter 上分享视频

iphone - ScrollView 中的 UIWebView 不会重绘

iphone - 如何找到天数

iphone - 我无法通过按回车键隐藏键盘

使用 R 在 Windows 上进行 ROauth

ios - 如何从动态创建的 UITextField 获取文本而不触摸或点击文本字段(禁用用户交互)?

iphone - 在播放声音的同时录制音频

twitter - Twitter 的 max_id 参数的默认值?