【问题标题】:iOS-How to get Twitter account access?iOS-如何获取 Twitter 账号访问权限?
【发布时间】:2013-09-13 14:35:47
【问题描述】:

我正在尝试使用 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
           }
      }

那么,我还需要做什么?任何评论,答案表示赞赏。

【问题讨论】:

    标签: iphone ios twitter


    【解决方案1】:

    会回答自己的问题,希望对其他人有所帮助。

    虽然我使用的是来自 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 已经过时了。
    【解决方案2】:

    我建议 Parse.com 更好、更轻松地实现 Twitter 和 Facebook 集成。只需几行代码,您就可以同时拥有它们,Parse 会完成其余的工作。

    【讨论】:

      猜你喜欢
      • 2015-11-25
      • 1970-01-01
      • 2020-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多