ios 如何通过 Twitter 授权用户登录

How to Authorise User login via twitter in ios

我正在 ios 应用程序中通过 twitter 实现登录。 我已经在开发者页面上创建了 Twitter 应用程序。 我有 twitter API 密钥和秘密。

现在我为 OAuth-Authorization 添加了 AFOAuth2Manager CHECK HEREAFnetworking 框架。

并在谷歌搜索时编写代码,按钮操作为

      #import "AFOAuth2Manager.h"

    - (IBAction)loginWithTwitterAction:(id)sender
    {
        NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/oauth/request_token"];
        AFOAuth2Client *oauthClient = [AFOAuth2Client clientWithBaseURL:url clientID:kClientID secret:kClientSecret];

        [oauthClient authenticateUsingOAuthWithURLString:@"/oauth/token" parameters:nil success:^(AFOAuthCredential *credential)
         {
             NSLog(@"I have a token! %@", credential.accessToken);
             [AFOAuthCredential storeCredential:credential withIdentifier:oauthClient.serviceProviderIdentifier];
         } failure:^(NSError *error) {
             NSLog(@"Error: %@", error);
         }];
    }

它不工作请更正me.and我可能错过了一些其他步骤。

您应该使用 Fabric.io,因为 Larcerax 向您推荐。当你安装它时,你将能够非常容易地使用 twitter 登录。我使用这些方法:

登录:

- (void)loginWithTwitter { 
    [[Twitter sharedInstance] logInWithCompletion:^(TWTRSession *session,
                                              NSError *error) {
    if (session) {
      NSLog(@"signed in as %@", [session userName]);
      [[NXTTwitterManager sharedInstance] requestTwtFollowersWithCursor:nil];
      [[NXTTwitterManager sharedInstance] requestTwtFollowingWithCursor:nil];
    } else {
      NSLog(@"error: %@", [error localizedDescription]);
    }
  }];
}

获得关注者:

- (void)requestTwtFollowersWithCursor:(NSString *)nextCursor {
  NSString *statusesShowEndpoint = [NSString
      stringWithFormat:@"%@%@",
                   @"https://api.twitter.com/1.1/followers/ids.json?",
                   (nextCursor.length > 0)
                       ? [NSString stringWithFormat:@"cursor=%@&count=5000",
                                                    nextCursor]
                       : @"count=5000"];
  NSDictionary *params = @{ @"id" : [Twitter sharedInstance].session.userID };
  NSError *clientError;

  NSURLRequest *request = [[[Twitter sharedInstance] APIClient]
      URLRequestWithMethod:@"GET"
                       URL:statusesShowEndpoint
                parameters:params
                     error:&clientError];

  if (request) {
    [[[Twitter sharedInstance] APIClient]
        sendTwitterRequest:request
                completion:^(NSURLResponse *response, NSData *data,
                         NSError *connectionError) {
                  if (data) {
                    NSError *jsonError;
                    NSDictionary *json =
                        [NSJSONSerialization JSONObjectWithData:data
                                                        options:0
                                                          error:&jsonError];
                    [self.followers addObjectsFromArray:json[@"ids"]];
                    if (![json[@"next_cursor_str"] isEqualToString:@"0"]) {
                      [[NXTTwitterManager sharedInstance]
                          requestTwtFollowersWithCursor:
                              json[@"next_cursor_str"]];
                    } else {
                      [self matchFriends];
                    }
                  } else {
                    NSLog(@"Error: %@", connectionError);
                    [self matchFriends];
                  }
                }];
  } else {
    NSLog(@"Error: %@", clientError);
  }
}

获取朋友(以下):

- (void)requestTwtFollowingWithCursor:(NSString *)nextCursor {
  NSString *statusesShowEndpoint = [NSString
          stringWithFormat:@"%@%@",
                   @"https://api.twitter.com/1.1/friends/ids.json?",
                   (nextCursor.length > 0)
                       ? [NSString stringWithFormat:@"cursor=%@&count=5000",
                                                    nextCursor]
                       : @"count=5000"];
  NSDictionary *params = @{ @"id" : [Twitter sharedInstance].session.userID };
  NSError *clientError;

  NSURLRequest *request = [[[Twitter sharedInstance] APIClient]
      URLRequestWithMethod:@"GET"
                       URL:statusesShowEndpoint
                parameters:params
                     error:&clientError];

  if (request) {
    [[[Twitter sharedInstance] APIClient]
        sendTwitterRequest:request
                completion:^(NSURLResponse *response, NSData *data,
                         NSError *connectionError) {
                  if (data) {
                    NSError *jsonError;
                    NSDictionary *json =
                        [NSJSONSerialization JSONObjectWithData:data
                                                        options:0
                                                          error:&jsonError];
                    [self.followings addObjectsFromArray:json[@"ids"]];
                    if (![json[@"next_cursor_str"] isEqualToString:@"0"]) {
                      [[NXTTwitterManager sharedInstance]
                          requestTwtFollowingWithCursor:
                              json[@"next_cursor_str"]];
                    } else {
                      [self matchFriends];
                    }
                  } else {
                    NSLog(@"Error: %@", connectionError);
                    [self matchFriends];
                  }
                }];
  } else {
    NSLog(@"Error: %@", clientError);
  }
}