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
48
[DroidKaigi 2024] Android ViewからJetpack Composeへ 〜Jetpack Compose移行のすゝめ〜 / From Android View to Jetpack Compose: A Guide to Migration
syarihu
4
3.7k
[shibuya.apk #46] Composableの枠を超えてアニメーションする / Animation beyond Composable
syarihu
0
55
[shibuya.apk #41] Jetpack Composeでグリッドに柔軟にスペースを入れたい
syarihu
0
3.3k
[DroidKaigi 2022] 詳解Google Playの新しい定期購入 ~オファーの活用や実装例を添えて~
syarihu
0
3.6k
5分で分かるGoogle Playの新しいサブスクリプション / Google Play new subscription 2022
syarihu
1
1.8k
[DroidKaigi 2021] Google Play 定期購入 比例配分モード完全攻略ガイド / Google Play Subscription Proration Mode Complete Guide
syarihu
1
7.8k
[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
P2P ではじめる WebRTC のつまづきどころ
tnoho
1
280
【CEDEC2025】現場を理解して実現!ゲーム開発を効率化するWebサービスの開発と、利用促進のための継続的な改善
cygames
PRO
0
430
MCPと認可まわりの話 / mcp_and_authorization
convto
2
310
サイバー攻撃のシミュレーション:攻撃者の視点からみる防御のむずかしさ!AWSで試してみよう / 20250423 Kumiko Hemmi
shift_evolve
PRO
1
150
AIに全任せしないコーディングとマネジメント思考
kikuchikakeru
0
280
モバイルゲームの開発を支える基盤の歩み ~再現性のある開発ラインを量産する秘訣~
qualiarts
0
780
解消したはずが…技術と人間のエラーが交錯する恐怖体験
lamaglama39
0
140
東京海上日動におけるセキュアな開発プロセスの取り組み
miyabit
0
200
経理出身PdMがAIプロダクト開発を_ハンズオンで学んだ話.pdf
shunsukenarita
1
240
AI によるドキュメント処理を加速するためのOCR 結果の永続化と再利用戦略
tomoaki25
0
170
Recoil脱却の現状と挑戦
kirik
3
480
Microsoft Learn MCP/Fabric データエージェント/Fabric MCP/Copilot Studio-簡単・便利なAIエージェント作ってみた -"Building Simple and Powerful AI Agents with Microsoft Learn MCP, Fabric Data Agent, Fabric MCP, and Copilot Studio"-
reireireijinjin6
1
170
Featured
See All Featured
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
15
1.6k
Designing Experiences People Love
moore
142
24k
Producing Creativity
orderedlist
PRO
346
40k
Agile that works and the tools we love
rasmusluckow
329
21k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.8k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
229
22k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
Adopting Sorbet at Scale
ufuk
77
9.5k
Large-scale JavaScript Application Architecture
addyosmani
512
110k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
130
19k
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
おしまい