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
66
[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
70
[shibuya.apk #41] Jetpack Composeでグリッドに柔軟にスペースを入れたい
syarihu
0
3.4k
[DroidKaigi 2022] 詳解Google Playの新しい定期購入 ~オファーの活用や実装例を添えて~
syarihu
0
3.7k
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
オブザーバビリティが育むシステム理解と好奇心
maruloop
3
1.9k
AI機能プロジェクト炎上の 3つのしくじりと学び
nakawai
0
180
GPUをつかってベクトル検索を扱う手法のお話し~NVIDIA cuVSとCAGRA~
fshuhe
0
310
激動の時代を爆速リチーミングで乗り越えろ
sansantech
PRO
1
220
Zero Trust DNS でより安全なインターネット アクセス
murachiakira
0
130
ソースを読む時の思考プロセスの例-MkDocs
sat
PRO
1
350
【SORACOM UG Explorer 2025】さらなる10年へ ~ SORACOM MVC 発表
soracom
PRO
0
200
SRE × マネジメントレイヤーが挑戦した組織・会社のオブザーバビリティ改革 ― ビジネス価値と信頼性を両立するリアルな挑戦
coconala_engineer
0
410
「タコピーの原罪」から学ぶ間違った”支援” / the bad support of Takopii
piyonakajima
0
160
20251029_Cursor Meetup Tokyo #02_MK_「あなたのAI、私のシェル」 - プロンプトインジェクションによるエージェントのハイジャック
mk0721
PRO
6
2.2k
20251027_マルチエージェントとは
almondo_event
1
500
AIとの協業で実現!レガシーコードをKotlinらしく生まれ変わらせる実践ガイド
zozotech
PRO
2
240
Featured
See All Featured
A Tale of Four Properties
chriscoyier
161
23k
The Cost Of JavaScript in 2023
addyosmani
55
9.1k
How To Stay Up To Date on Web Technology
chriscoyier
791
250k
Producing Creativity
orderedlist
PRO
348
40k
Practical Orchestrator
shlominoach
190
11k
Designing for humans not robots
tammielis
254
26k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
22k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Gamification - CAS2011
davidbonilla
81
5.5k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.2k
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
おしまい