iOS-如何获得Twitter帐户访问权限? [英] iOS-How to get Twitter account access?

查看:316
本文介绍了iOS-如何获得Twitter帐户访问权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用twitter api并获得回复

I am trying to use twitter api and getting response

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

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

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

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

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

So,what I need to do additionally?any comment,answer is appreciated.

推荐答案

回答自己questio n,可能对其他人有帮助。

Gonna answer own question,may it helps to other.

虽然我在twitter网站上使用示例 https://dev.twitter.com/docs/ios/making-api-requests-slrequest ACAccountStore 对象分配。

Though I am using example from twitter site https://dev.twitter.com/docs/ios/making-api-requests-slrequest It works fine except the ACAccountStore object allocation.

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

I just re-allocate the ACAccountStore wherever I require.

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

这篇关于iOS-如何获得Twitter帐户访问权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆