2
- (IBAction)loginToTwitter:(id)sender {
    self.accountStore = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

    [self.accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
        if(granted) {
            NSArray *accounts = [self.accountStore accountsWithAccountType:accountType];

            if ([accounts count] > 0) {
                ACAccount *twitterAccount = [accounts objectAtIndex:0];
                NSLog(@"User Name: %@",twitterAccount.username);
                NSLog(@"Account Type: %@",twitterAccount.accountType);
                NSArray *userID = [[accounts valueForKey:@"properties"] valueForKey:@"user_id"];

                NSString *url_users_show = [NSString stringWithFormat:@"https://api.twitter.com/1.1/users/show.json?user_id=%@",[userID objectAtIndex:0]];

                SLRequest *getRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:[NSURL URLWithString:url_users_show] parameters:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%@",[userID objectAtIndex:0]] forKey:@"user_id"]];

                getRequest.account = twitterAccount;

                [getRequest performRequestWithHandler:^(NSData *responseData,
                                                        NSHTTPURLResponse *urlResponse, NSError *error)
                 {
                     if(responseData) {
                         NSLog(@"Twitter HTTP response: %i", [urlResponse statusCode]);
                         NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
                         if(responseDictionary) {
                             NSLog(@"Response: %@", responseDictionary);
                             if ([responseDictionary objectForKey:@"errors"]) {
                                 dispatch_async(dispatch_get_main_queue(), ^{
                                     [self showAlert: @"Twitter": [[[responseDictionary objectForKey:@"errors"] objectAtIndex:0] objectForKey:@"message"]];
                                 });
                             }
                         }
                     } else {
                         // responseDictionary is nil
                         dispatch_async(dispatch_get_main_queue(), ^{
                             [self showAlert: @"Twitter": @"Unable to authenticate you"];
                         });
                     }
                 }];
            }
        } else {
            //Failed
            NSLog(@"error getting permission %@",error);
            dispatch_async(dispatch_get_main_queue(), ^{
                [self showAlert: @"No Twitter Account Detected": @"Please go into your device's settings menu to add your Twitter account."];
            });
        }
    }];
}

上面是我的代码,下面是我控制台上的输出。

输出:

Twitter HTTP response: 401
Response: {
    errors =     (
                {
            code = 32;
            message = "Could not authenticate you";
        }
    );
}

我想从 Twitter 获取用户数据,例如名字、姓氏、个人资料图片、登录 ID。

提前致谢...

4

0 回答 0