Amazon Cognito の Twitter 連係を iOS アプリから使ってみた

2015.05.01

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

待ちに待った Twitter 認証!

本日、Amazon Cognito にアップデートがあり、Cognito Identity に待ちに待った Twitter / Digits 認証がサポートされました!概要についてはせーのがまとめておりますので、こちらをご参照ください。

ということで、早速使ってみました。

Twitter のアプリの作成

まず Twitter のアプリを作成しましょう。Twitter をモバイルアプリから使用するには、現在は Fabric というサービスを通して利用するようになっています。アカウントの登録方法は下記のブログにまとめていますので、参照にしながら登録しましょう。Fabric は、利用申請を行い、承認されてから利用できるようになります。もし実際にアプリで利用したい場合、登録までに少し時間がかかることを念頭に置いておきましょう。

登録が完了したら、Fabric の Web コンソールから Consumer Key と Secret Key をメモしておきましょう。

cognito-twitter01

Congito Identity Pool の作成

次に Cognito の IdentityPool を作成します。作成方法は下記を参考にしてください。

上記ブログの ③ Public Identity Providers のところが、今回から新しくなりました。タブに Twitter / Digits が増えています。ここは Twitter の Consumer Key と Consumer Secret を入力します。

cognito-twitter02

あとはアプリ側の実装です。上記手順で TwitterKit はインポートできているはずですので、次に AWS Mobile SDK をインポートしましょう。

pod 'AWSCore'

次に、適当な View Controller に認証処理を実装しましょう。

#import "ViewController.h"
#import <TwitterKit/TwitterKit.h>
#import <AWSCore/AWSCognitoIdentity.h>

@interface ViewController ()

@property (weak, nonatomic) TWTRLogInButton *logInButton;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // TWTRLogInButtonの設定
    TWTRLogInButton *logInButton = [TWTRLogInButton buttonWithLogInCompletion:^(TWTRSession *session, NSError *error) {
        if (error) {
            NSLog(@"Error : %@", error);
        } else {
            NSLog(@"UserName : %@", session.userName);
            self.logInButton.hidden = YES;
            NSString *value = [NSString stringWithFormat:@"%@;%@", session.authToken, session.authTokenSecret];
            [self getIdentityId:value];
        }
    }];
    logInButton.center = self.view.center;
    self.logInButton = logInButton;
    [self.view addSubview:logInButton];
}

- (void)getIdentityId:(NSString *)identity
{
    NSDictionary *logins = @{@"api.twitter.com":identity};
    AWSCognitoCredentialsProvider *provider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:AWSRegionUSEast1
                                                                                             identityId:nil
                                                                                         identityPoolId:@"us-east-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
                                                                                                 logins:logins];
    [[provider getIdentityId] continueWithExecutor:[BFExecutor mainThreadExecutor] withBlock:^id(BFTask *task) {
        if (task.cancelled || task.faulted) {
            NSLog(@"Error");
        } else {
            NSString* cognitoId = provider.identityId;
            NSLog(@"cognitoId: %@", cognitoId);
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                            message:cognitoId
                                                           delegate:nil
                                                  cancelButtonTitle:nil
                                                  otherButtonTitles:@"OK", nil];
            [alert show];
        }
        return nil;
    }];
}

以上で終わりです。アプリを実行すると、Twitter 認証が表示され…

cognito-twitter04

Cognito の IdentityId が取得できました!

cognito-twitter05

まとめ

Twitter ログインは国内では特に必須とも言える機能なので、Twitter 認証だけで AWS Credentials が得られるのは非常に有難いですね!ぜひ使ってみてください。

参考