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
42
[DroidKaigi 2024] Android ViewからJetpack Composeへ 〜Jetpack Compose移行のすゝめ〜 / From Android View to Jetpack Compose: A Guide to Migration
syarihu
3
3.6k
[shibuya.apk #46] Composableの枠を超えてアニメーションする / Animation beyond Composable
syarihu
0
53
[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
580
[Android 11 Meetups] Google Play Commerce からのアップデート / Android 11 Meetups Google Play Commerce
syarihu
4
2.9k
Other Decks in Technology
See All in Technology
Snowflake Summit 2025全体振り返り / Snowflake Summit 2025 Overall Review
mtpooh
2
440
Amazon Bedrockで実現する 新たな学習体験
kzkmaeda
2
680
250627 関西Ruby会議08 前夜祭 RejectKaigi「DJ on Ruby Ver.0.1」
msykd
PRO
2
370
Core Audio tapを使ったリアルタイム音声処理のお話
yuta0306
0
150
Tech-Verse 2025 Global CTO Session
lycorptech_jp
PRO
0
1.2k
Liquid Glass革新とSwiftUI/UIKit進化
fumiyasac0921
0
300
より良いプロダクトの開発を目指して - 情報を中心としたプロダクト開発 #phpcon #phpcon2025
bengo4com
1
3.2k
論文紹介:LLMDet (CVPR2025 Highlight)
tattaka
0
240
KubeCon + CloudNativeCon Japan 2025 Recap by CA
ponkio_o
PRO
0
250
Node-REDのFunctionノードでMCPサーバーの実装を試してみた / Node-RED × MCP 勉強会 vol.1
you
PRO
0
130
タイミーのデータモデリング事例と今後のチャレンジ
ttccddtoki
4
1.4k
「良さそう」と「とても良い」の間には 「良さそうだがホンマか」がたくさんある / 2025.07.01 LLM品質Night
smiyawaki0820
1
430
Featured
See All Featured
Become a Pro
speakerdeck
PRO
28
5.4k
Build The Right Thing And Hit Your Dates
maggiecrowley
36
2.8k
Adopting Sorbet at Scale
ufuk
77
9.4k
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
GraphQLの誤解/rethinking-graphql
sonatard
71
11k
Java REST API Framework Comparison - PWX 2021
mraible
31
8.7k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
20
1.3k
Why You Should Never Use an ORM
jnunemaker
PRO
58
9.4k
Making the Leap to Tech Lead
cromwellryan
134
9.4k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.4k
Docker and Python
trallard
44
3.5k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
30
2.1k
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
おしまい