Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
New Google Sign-In API
Search
syarihu
December 04, 2015
Technology
0
1.3k
New Google Sign-In API
Google Play Services 8.3で新しく出てきたGoogle Sign-In APIの話と、僕が以前作ったOAuthHelperの話です。
syarihu
December 04, 2015
Tweet
Share
More Decks by syarihu
See All by syarihu
Recap #io25 What's new in Google Play
syarihu
0
69
[DroidKaigi 2024] Android ViewからJetpack Composeへ 〜Jetpack Compose移行のすゝめ〜 / From Android View to Jetpack Compose: A Guide to Migration
syarihu
4
4k
[shibuya.apk #46] Composableの枠を超えてアニメーションする / Animation beyond Composable
syarihu
0
72
[shibuya.apk #41] Jetpack Composeでグリッドに柔軟にスペースを入れたい
syarihu
0
3.4k
[DroidKaigi 2022] 詳解Google Playの新しい定期購入 ~オファーの活用や実装例を添えて~
syarihu
0
3.8k
5分で分かるGoogle Playの新しいサブスクリプション / Google Play new subscription 2022
syarihu
1
1.9k
[DroidKaigi 2021] Google Play 定期購入 比例配分モード完全攻略ガイド / Google Play Subscription Proration Mode Complete Guide
syarihu
1
7.9k
[Money Forward Tech Drive] What's new in Google Play [Recap #io21]
syarihu
0
590
[Android 11 Meetups] Google Play Commerce からのアップデート / Android 11 Meetups Google Play Commerce
syarihu
4
2.9k
Other Decks in Technology
See All in Technology
データ組織ゼロから投資を得るまでの軌跡と未来図 〜AIの前にやるべきこと〜 / Building a Data Organization from Scratch: The Journey to Securing Investment and a Vision for the Future
kaonavi
0
110
Master Dataグループ紹介資料
sansan33
PRO
1
3.9k
Pythonで構築する全国市町村ナレッジグラフ: GraphRAGを用いた意味的地域検索への応用
negi111111
8
3.2k
CloudComposerによる大規模ETL 「制御と実行の分離」の実践
leveragestech
0
210
The Twin Mandate of Observability
charity
1
1.2k
エンジニアにとってコードと並んで重要な「データ」のお話 - データが動くとコードが見える:関数型=データフロー入門
ismk
0
350
今日から使える AWS Step Functions 小技集 / AWS Step Functions Tips
kinunori
5
630
エンジニア採用と 技術広報の取り組みと注力点/techpr1112
nishiuma
0
110
Playwrightで始めるUI自動テスト入門
devops_vtj
0
260
Design and implementation of "Markdown to Google Slides" / phpconfuk 2025
k1low
1
370
自己的售票系統自己做!
eddie
0
310
激動の2025年、Modern Data Stackの最新技術動向
sagara
0
1.2k
Featured
See All Featured
Faster Mobile Websites
deanohume
310
31k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
2
310
KATA
mclloyd
PRO
32
15k
The World Runs on Bad Software
bkeepers
PRO
72
12k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
GraphQLとの向き合い方2022年版
quramy
49
14k
A designer walks into a library…
pauljervisheath
210
24k
Music & Morning Musume
bryan
46
6.9k
Bash Introduction
62gerente
615
210k
GraphQLの誤解/rethinking-graphql
sonatard
73
11k
Speed Design
sergeychernyshev
32
1.2k
Learning to Love Humans: Emotional Interface Design
aarron
274
41k
Transcript
New Google Sign-In API 2015/12/04 (Fri) shibuya.apk #5 @syarihu
GMO Media, Inc. Engineer - Android - Server Side Java
@syarihu (しゃりふ)
では、さっそく
New Google Sign-In API
の前に
以前のGoogle Sign-Inの話から…
Previous Google Sign-In... - アカウント選択画面 ( AccountManager.newChooseAccountIntent ) - 自分でIntent投げて、
返ってきたらごにょごにょやって… - AuthTokenの取得 ( AccountManager#getAuthToken ) - 認証状態の確認 - 認証されてなかったら認証許可ダイアログを表示 - 認証されてたらAuthTokenを取得 - 認証許可が外れてたら再取得も自分で…
Previous Google Sign-In... - アカウントの選択でつかうメソッド AccountManager.newChooseAccountIntent(Account selectedAccount, ArrayList<Account> allowableAccounts, String[]
allowableAccountTypes, boolean alwaysPromptForAccount, String descriptionOverrideText, String addAccountAuthTokenType, String[] addAccountRequiredFeatures, Bundle addAccountOptions) - AuthTokenの取得でつかうメソッド AccountManager#getAuthToken(Account account, String authTokenType, Bundle options, boolean notifyAuthFailure, AccountManagerCallBack<Bundle> callback, Handler handler)
(´゚д゚`)
なんだこれ。。
引数多い、みにくい、つらい。。
これ、実際に使おうと思うと結構たいへん。。
なので
OAuthHelperとかいうのを作ってみた
OAuthHelperとは - アカウント選択 - 認証許可確認 - AuthTokenの取得 - 再認証
OAuthHelperとは - アカウント選択 - 認証許可確認 - AuthTokenの取得 - 再認証 これらのめんどうな処理を
まとめてやってくれます
使い方は簡単
1. OAuthHelper.OnAuthListenerを実装 public class MainActivity extends ActionBarActivity implements OAuthHelper.OnAuthListener {
@Override public void getAuthToken(String authToken) { // ここにAuthTokenを使った処理を書く } }
2. OAuthHelperのインスタンスを生成 public class MainActivity extends ActionBarActivity implements OAuthHelper.OnAuthListener {
@Override protected void onCreate(Bundle savedInstanceState) { ... // Activity activity, String Scopes, OnAuthListener listener mHelper = new OAuthHelper( this, "oauth2:" + UrlshortenerScopes.URLSHORTENER, this ); ... } }
3. Intentから戻ってきた時の処理 ... @Override protected void onActivityResult(int requestCode, int resultCode,
Intent data) { super.onActivityResult(requestCode, resultCode, data); // アカウント選択や認証画面から返ってきた時の処理を OAuthHelperで受け取る mHelper.onActivityResult(requestCode, resultCode, data); } ...
4. 認証開始 mHelper.startAuth(false);
4. 認証開始 mHelper.startAuth(false); ↑ これの引数はAuthTokenの再取得するかどうか
実行するとこうなります
実行するとこうなります
実行するとこうなります
よし!
これでGoogle Sign-Inを実装するのが楽になったぞ!
ε=\_〇ノヒャッホーイ!!
が…
新しいGoogle Sign-In APIが出たので
作って半年で要らない子に。。
il||li_◦/ ̄|_il||li ナンテコッタィ
まぁそんな子は放っておいて
新しいGoogle Sign-In APIを使いましょう
New Google Sign-In API - アカウントを取得してサインインするのに権限が必要ない - 新しくデザインされたサインインボタン - Slient
Sign-In - 実装も以前より簡単
こんな感じの画面
こんな感じの画面
こんな感じの画面
こんな感じの画面
Google Sign-In APIの実装 - GoogleSignInOptionsオブジェクトを作成 - GoogleApiClientオブジェクトを作成 - Googleサインインボタンを追加する -
サインイン処理の実装
1. GoogleSignInOptionsオブジェクトを作成する // 許可させたいAPIのスコープ mScope = new Scope("https://www.googleapis.com/auth/urlshortener"); mGoogleSignInOptions =
new GoogleSignInOptions .Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestScopes(mScope) .requestEmail() .build();
2. GoogleApiClientオブジェクトを作成する mGoogleApiClient = new GoogleApiClient.Builder(this) /* FragmentActivity, OnConnectionFailedListener */
.enableAutoManage(this, this) .addApi(Auth.GOOGLE_SIGN_IN_API, mGoogleSignInOptions) .addScope(mScope) .build();
3. Googleサインインボタンを追加する <com.google.android.gms.common.SignInButton android:id="@+id/sign_in_button" android:layout_width="wrap_content" android:layout_height="wrap_content" />
3. Googleサインインボタンを追加する SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button); signInButton.setOnClickListener(this); // セットすると、スコープに適したボタンになる
signInButton.setScopes(mGoogleSignInOptions.getScopeArray()); signInButton.setSize(SignInButton.SIZE_WIDE);
ScopeがGoogle+だとこんな感じ 3. Googleサインインボタンを追加する
4. サインイン処理の実装 private void signIn() { // サインイン用のIntentを取得してIntentを投げる Intent signInIntent
= Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); startActivityForResult(signInIntent, RC_SIGN_IN); } @Override public void onClick(View v) { if(v.getId() == R.id.sign_in_button) { signIn(); } }
4. サインイン処理の実装 @Override public void onActivityResult(int requestCode, int resultCode, Intent
data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == RC_SIGN_IN) { // サインインした結果を取得 GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); handleSignInResult(result); } }
4. サインイン処理の実装 private void handleSignInResult(GoogleSignInResult result) { Log.d(TAG, "handleSignInResult:" +
result.isSuccess()); if (result.isSuccess()) { // サインインが成功したら、サインインボタンを消して、ユーザー名を表示する mGoogleSignInAccount = result.getSignInAccount(); mStatusTextView.setText( "DisplayName: " + mGoogleSignInAccount.getDisplayName() ); } }
5. サインアウト処理の実装 private void signOut() { Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback( new ResultCallback<Status>() {
@Override public void onResult(Status status) { showSnackBar(getString(R.string.signed_out)); } }); }
6. OAuthTokenの取得 accessToken = GoogleAuthUtil.getToken( MainActivity.this, new Account(mGoogleSignInAccount.getEmail(), "com.google"), "oauth2:"
+ API_SCOPE );
7. Silent Sign-In OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn (mGoogleApiClient); if (opr.isDone())
{ // サインイン成功時 GoogleSignInResult result = opr.get(); handleSignInResult(result); } else { opr.setResultCallback(new ResultCallback<GoogleSignInResult>() { @Override public void onResult(GoogleSignInResult googleSignInResult) { // 未サインイン時 } }); }
試しにこれを使ってGoogle Url Shortenerで URLを短縮するサンプルを作ってみました
Google Sign-In API Sample https://youtu.be/_BpuYe8w4PI
新しいGoogle Sign-In APIを使えば
こんなに手軽に Google Sign-Inを実装できる!
すばらしい!
OAuthHelperってなんだよ!誰もしらねえよ!!
あんな誰も知らない子のことは忘れて
みなさんぜひ新しいGoogle Sign-In APIを使いましょう
サンプルコードとか - 【Androidアプリ開発】AndroidでのOAuth認証が簡単に実装できる OAuthHelperを作ってみた http://diary.syarihu.net/2015/05/androidandroidoauthoauthhelper.html - [GitHub] OAuthHelperSample https://github.com/syarihu/OAuthHelperSample -
[GitHub] GoogleSignInTest https://github.com/syarihu/GoogleSignInTest
参考 - Integrating Google Sign-In into Your Android App https://developers.google.com/identity/sign-in/android/sign-in
- Google Sign-In Quickstart https://github.com/googlesamples/google- services/tree/master/android/signin
おしまい