- (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";
}
);
}
名、姓、プロフィール写真、ログイン ID などのユーザー データを Twitter から取得したいと考えています。
前もって感謝します...